评论this question让我想知道。
在 JavaScript 中,是否可以获取 HTML 属性的“原始”值,即解析前在源代码中的写入方式?
假设您有这个 HTML:
<section id="theSection" title="The section">The section</section>
<script>
console.log(document.getElementById('theSection').title);
</script>
我需要在脚本中写什么才能让它输出标题的原始转义值,而不是解析后的值?
JavaScript 节点有很多属性和值,但在本例中,没有一个是 "The section"
而不是 "The section"
。
最佳答案
无法像源代码那样在浏览器中获取已解析的 Unicode 字符
HTML 文件和  
是一个空格然后我们可以使用这个解决方法来解决任何标题在其单词之间有空格的问题。
<section id="theSection" title="The section">The section</section>
<script>
console.log(document.getElementById('theSection').title.split(' ').join(' '));
</script>
我知道这是愚蠢的回答,
但如果真的打算处理空间 unicode 然后点击具有此 <section id="theSection" title="The section">The section</section>
的 url与 fetch(url)
如下所示:
fetch('stackoverflow.com')
.then(async (res) => {
const text = await res.text();
// process the text with any unicode charachter as it is now 100% string
}
);
现在源 HTML 文件是一个字符串。在您的情况下,最好在带有正则表达式的字符串上使用 javascript 来查找您想要在 html 源中处理的内容。
关于javascript - 您能否从源代码中显示的属性中检索值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53437564/