简体   繁体   English

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

[英]Escaping double double quotes in html attributes in Javascript

I use bootstrap and create html nodes which contain popovers with jQuery. 我使用bootstrap并创建包含jQuery弹出窗口的html节点。 The popovers contain html so that I have problems with the quotes and escaping: 弹出窗口包含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>';

How can I escape the double quotes inside the attributes => html-attributes? 如何在attributes => html-attributes中转义双引号? in the data-content attribute correctly? 在数据内容属性中正确吗?

Edit: I compile the code with angular so that it should also work with it. 编辑:我使用angular编译代码,以便它也可以使用它。

If it is HTML, you can use HTML entities to escape characters that might be interpreted otherwise. 如果是HTML,则可以使用HTML实体来转义可能以其他方式解释的字符。 For example, &quot; 例如, &quot; will be displayed as " in HTML attributes: 将显示为"在HTML属性中:

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

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

Refer to W3C's HTML entities chart for more information: http://dev.w3.org/html5/html-author/charref 有关更多信息,请参阅W3C的HTML实体图表: http//dev.w3.org/html5/html-author/charref

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM