【发布时间】:2015-10-22 19:36:20
【问题描述】:
我正在使用 jQuery 2.1.4 我有一个使用 MVC5 Razor 视图助手渲染的 html 元素的弹出窗口:
<a href="javascript:void(0);" class="btn" rel="popover" data-placement="left" data-original-title="@benefit.Name" data-html="true" data-content='@Html.Raw(benefit.ShortDescription)'>
@benefit.Name<br />
我在数据内容中的 html 周围加上了单引号,这意味着它可以很好地呈现在页面上。 但是,我正在使用 DataTables 并尝试导出所有数据并从中去除 html 标签。
这是视图呈现的内容:
<a href="javascript:void(0);" class="btn" rel="popover" data-placement="left" data-original-title="Benefits for Practitioners" data-html="true" data-content="Credit Paraplanning is designed to assist the Practitioners clients utilise appropriate finance options: <br />
<ul>
<li><span style="font-size: 13px;">Maximising the range of services the Practitioner is seen to provide access to, </span></li>
<li><span style="font-size: 13px;">Saving time in research and preparing applications</span></li>
</ul>">
Benefits for Practitioners<br>
</a>
所以即使我指定了单引号,它也会呈现在双引号中。
现在,当我尝试获取 html 以去除 html 标记时,我得到一个 jquery 错误:
"Syntax error, unrecognized expression: Credit Paraplanning is designed to assist the Practitioners clients utilise appropriate finance options: <br ></a>↵<ul>↵<li><span style="font-size: 13px;">Maximising the range of services the Practitioner is seen to provide access to, </span></li>↵<li><span style="font-size: 13px;">Saving time in research and preparing applications</span></li>↵</ul>"
代码如下:
var datacontent = $(value).data("content");
if (datacontent === null) {
return '';
}
if ($(datacontent).text()) {// Throws error here.
// Get the string without html
}
我可以理解这个 $(datacontent) 然后会导致错误。但是我怎样才能解决这个问题呢? 如何去除 html 标签?
【问题讨论】:
标签: javascript jquery html twitter-bootstrap asp.net-mvc-5