我使用的是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标签?
答案 0 :(得分:1)
我解决这个问题的方法是从隐藏的div加载内容,而不是在锚的data-content属性中设置它。
<a href="javascript:void(0);" class="btn" rel="popover" data-placement="left" data-original-title="@benefit.Name" data-html="true">
@benefit.Name<br />
</a>
<div class="content" style="display:none;">
@Html.Raw(benefit.ShortDescription)
</div>
然后在javascript:
$('[rel="popover"]').popover({
html: true,
content: function () {
return $($(this).next('.content')).html();
}
});