在属性内存储HTML [英] Storing HTML Within Attribute
本文介绍了在属性内存储HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将HTML代码存储在这样的element属性中.
I'm storing HTML code within an element attribute like so.
<div data-html-code="<iframe width="560" height="315" src="https://www.youtube.com/embed/sNhhvQGsMEc" frameborder="0" allowfullscreen></iframe>"></div>
如何使用jQuery/Javascript转义所有必需的字符以使其有效?
How can I escape all the necessary characters to make this valid using jQuery/Javascript?
推荐答案
使用此htmlEscape
方法
function htmlEscape(str) {
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
这应该给您一个可以用作有效html属性值的字符串
this should give you a string that can be used as a valid html attribute value
这篇关于在属性内存储HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文