【问题标题】:ASP.NET MVC5 Bootstrap Popover string with double quotes inside single quotesASP.NET MVC5 Bootstrap Popover 字符串,单引号内有双引号
【发布时间】: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=&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 标签?

【问题讨论】:

    标签: javascript jquery html twitter-bootstrap asp.net-mvc-5


    【解决方案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();
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 2017-02-26
      相关资源
      最近更新 更多