我正在使用 jQuery 2.1.4 我有一个处理 html 元素的弹出窗口,该元素是使用 MVC5 Razor View 助手渲染的:
<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 标签。
这就是 View 呈现的内容:
<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 标签?
最佳答案
我解决这个问题的方法是从隐藏的 div 中加载内容,而不是将其设置在 anchor 的 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();
}
});
关于javascript - ASP.NET MVC5 Bootstrap Popover 字符串,单引号内有双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31740818/