html属性中的引号被标记为"来自element.html()的结果

时间:2014-11-24 05:14:43

标签: javascript jquery html

我有以下html:

<div id="ma-uitemplates-container">
    <button id="ExecuteAbort" 
        type="button" 
        class="btn btn-default" 
        data-mistro-command="Execute" 
        data-mistro-events='[{"SE":"click","MEM":[{"MEC":"NotificationManager","ME":"DismissNotification"}]}]'>
        Yes
    </button>
</div>

属性data-mistro-events包含一些我在代码中提取并解析为对象的JSON数据。

我现在正在编写一些代码,将一些标记从html页面保存到数据库中作为模板。

当我使用jquery获取此html:

$('#ma-uitemplates-container').html();

它给出了以下结果:

<button id="ExecuteAbort" type="button" class="btn btn-default" data-mistro-command="Execute" data-mistro-events="[{&quot;SE&quot;:&quot;click&quot;,&quot;MEM&quot;:[{&quot;MEC&quot;:&quot;NotificationManager&quot;,&quot;ME&quot;:&quot;DismissNotification&quot;}]}]">
    Yes
</button>

你可以看到jquery转换了我对属性data-mistro-events =&#39;&#39;&#39;&#39;要双引号data-mistro-events =&#34;&#34;,然后将属性值中的所有双引号转换为&#34;。

我怎样才能不这样做,而是按照我在页面中写的那样编写html?

此致

斯科特

1 个答案:

答案 0 :(得分:-1)

使用单引号编写html标记属性是无效的,这就是浏览器使用双引号转换data-mistro-events属性并且您的html正在破坏的原因。你必须使用双引号(“)而不是单引号(')的html属性,并用单引号写你的json。

尝试以下操作,这样可以正常使用

<div id="ma-uitemplates-container">
  <button id="ExecuteAbort" 
    type="button" 
    class="btn btn-default" 
    data-mistro-command="Execute" 
    data-mistro-events="[{'SE':'click','MEM':[{'MEC':'NotificationManager','ME':'DismissNotification'}]}]">
    Yes
  </button>
</div>