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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25093960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:56:11  来源:igfitidea点击:

Escaping double double quotes in html attributes in Javascript

javascriptangularjsescaping

提问by daniel

I use bootstrap and create html nodes which contain popovers with jQuery. The popovers contain html so that I have problems with the quotes and escaping:

我使用引导程序并使用 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>';

How can I escape the double quotes inside the attributes => html-attributes? in the data-content attribute correctly?

如何转义属性 => html-attributes 中的双引号?在数据内容属性中正确吗?

Edit:I compile the code with angular so that it should also work with it.

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

回答by Terry

If it is HTML, you can use HTML entities to escape characters that might be interpreted otherwise. For example, &quot;will be displayed as "in HTML attributes:

如果它是 HTML,您可以使用 HTML 实体来转义可能会以其他方式解释的字符。例如,&quot;"在 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