如何剥离字符串中的html标签,然后转义纯字符串并将转义的字符串放入html标签中
我想剥离 html 标签,然后转义纯字符串,并将转义字符串放回 html 标签,为此我尝试过:
var aa = "<p>hi testing & </p>";
var bb = $(aa).text();
var cc = escape(bb);
var dd = aa.replace(bb, cc);
dd 给我输出 hi%20testing%20%26 ,但问题是当我在字符串中有多个 html 标签时,例如: "
aa ="
hi testing &
失败
"
它不起作用。
请帮忙。
提前致谢。
I wanted to strip html tags then escape plain string and put back the escaped string back into html tags , for that i have tried :
var aa = "<p>hi testing & </p>";
var bb = $(aa).text();
var cc = escape(bb);
var dd = aa.replace(bb, cc);
dd gives me the output "<p>hi%20testing%20%26</p>"
, but the problem is when i have multiple html tags in string for eg: aa ="<p>hi testing & </p><p> failed & </p>"
its not working.
Please help.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你很接近!
jQuery.text()
对此非常方便。当您向其传递一个函数时,它将用该函数的结果替换所选元素的文本内容:You were close!
jQuery.text()
is quite handy for this. When you pass it a function it'll replace the text content of selected elements with the result of the function: