代码之家  ›  专栏  ›  技术社区  ›  daniel

Javascript中html属性中的转义双引号

  •  8
  • daniel  · 技术社区  · 10 年前

    我使用bootstrap并使用jQuery创建包含弹出窗口的html节点。弹出窗口包含html,因此我在引号和转义方面有问题:

    var content = '<input type="text" ng-model="test1" />';
    var txt =     '<button type="button" data-container="body" ' +
                         'data-toggle="popover" title="myTitle" data-html="true" '+
                         'data-content="'+content+'">Click</button>';
    

    如何转义属性中的双引号=>html属性?数据内容属性是否正确?

    编辑: 我用angular编译代码,这样它也可以使用它。

    1 回复  |  直到 10 年前
        1
  •  17
  •   Terry    10 年前

    如果是HTML,则可以使用HTML实体来转义可能会被其他方式解释的字符。例如 &quot; 将显示为 " 在HTML属性中:

    var content = '<input type=&quot;text&quot; ng-model=&quot;test1&quot; />';
    

    概念证明: http://jsfiddle.net/teddyrised/5X5Ye/

    有关更多信息,请参阅W3C的HTML实体图表: http://dev.w3.org/html5/html-author/charref