简体   繁体   English

如何在不使用Javascript编码标签本身的情况下HTML编码可能具有html标签的文本

[英]How can I HTML encode text that may have html tags without encoding the tags themselves in Javascript

Basically I have some text that may contain HTML tags but may also contain non-HTML encoded characters. 基本上,我有一些文本可能包含HTML标记,但也可能包含非HTML编码的字符。

var doc = window.document.implementation.createDocument
    ('http://www.w3.org/1999/xhtml', 'html',  null);
var text = '<head><script>somejs</script>' +
      '<script>var x = 7; var y = 5; var foo = x < y;</script>' +
      '</head><body></body>');

I wish to set text to an elements innerHTML attribute. 我希望将文本设置为一个元素innerHTML属性。 If I just do 如果我只是做

doc.getElementsByTagName('html')[0].innerHTML = text;

This causes a INVALID_STATE_ERR: DOM Exception 11 because of the less than sign between x and y. 由于x和y之间的小于号,这将导致INVALID_STATE_ERR:DOM异常11。

However, if I htmlEncode the variable text I get 但是,如果我对可变文本进行htmlEncode,则会得到

&lt;head&gt;&lt;script&gt;somejs&lt;/script&gt;&lt;script&gt;var x = 7; var y = 5; var     foo = x &lt; y;&lt;/script&gt;&lt;/head&gt;&lt;body&gt;&lt;/body&gt;

And thus I lose all the tags which I need for the element to behave as desired once its innerHTML has been set. 因此,一旦设置了innerHTML,我就失去了元素所需的所有标记,这些标记才能使元素表现出所需的行为。 Is there any standard way to htmlencode the contents of all the tags in a string without encoding the tags themselves? 有没有一种标准的方法可以对字符串中所有标签的内容进行html编码而不对标签本身进行编码?

For inline scripts you need to escape the string </script when followed by a space character, > , or / , else it would close the respective opening tag . 对于内联脚本,当后跟一个空格字符>/ ,需要转义字符串</script ,否则它将关闭相应的开始标记

So, instead of </script> , you could use <\\/script> . 因此,代替</script>您可以使用<\\/script> This works: 这有效:

var text = '<head><script>somejs<\/script><script>var x = 7; var y = 5; var foo = x < y;<\/script></head><body></body>';
el.innerHTML = text;

Update: Now that you've edited your question, I see you're using XHTML! 更新:现在,您已经编辑了问题,我看到您正在使用XHTML! That explains it — innerHTML doesn't work in XHTML. 这就解释了– innerHTML在XHTML中不起作用。

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

相关问题 如何在html标签之间替换内容而不替换标签本身 - How to replace content between html tags without replacing the tags themselves 在JavaScript中,如何在不影响标记的情况下替换HTML页面中的文本? - In JavaScript, how can I replace text in an HTML page without affecting the tags? 在javascript中编码/解码html标签 - Encode/decode html tags in javascript 我如何在 javascript 中使用 html 标签 - How can i use html tags in javascript 如何使用javascript访问html中父元素的多类子标签和子标签本身? - how to access multiple class of child tags and the child tags themselves of a parent element in html using javascript? 如何在不影响使用 Nodejs 或 Javascript 的 HTML 标签的情况下从 HTML 中获取 100 到 200 个单词? - How can I get 100 to 200 words from HTML without affecting HTML tags using Nodejs or Javascript? 如何扫描一段文本并将“文本后面”的所有超链接替换为HTML属性标签? - How can I get a piece of text scanned and have all hyperlinks “behind the text” replaced with HTML attribute tags? 我如何在没有 HTML 标签的情况下显示这些格式化文本 - How do i display these formated text without HTML tags 哪些HTML标记可能包含javascript代码? - What HTML tags may contain javascript code? 如何替换包含HTML标签的单词而不丢失HTML标签? - How can I replace words that contain HTML tags without losing the HTML tags?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM