我在数据内容中的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=&quot;font-size: 13px;&quot;>Maximising the range of services the Practitioner is seen to provide access to, </span></li>
<li><span style=&quot;font-size: 13px;&quot;>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加载内容,而不是在锚点的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();
    }
});
查看全文
  相关解决方案