javascript - 您能否从源代码中显示的属性中检索值?

标签 javascript html dom

评论this question让我想知道。

在 JavaScript 中,是否可以获取 HTML 属性的“原始”值,即解析前在源代码中的写入方式?

假设您有这个 HTML:

<section id="theSection" title="The&#32;section">The section</section>

<script>
 console.log(document.getElementById('theSection').title);
</script>
 

我需要在脚本中写什么才能让它输出标题的原始转义值,而不是解析后的值?
JavaScript 节点有很多属性和值,但在本例中,没有一个是 "The section" 而不是 "The section"

最佳答案

无法像源代码那样在浏览器中获取已解析的 Unicode 字符 HTML 文件和 &#32;是一个空格然后我们可以使用这个解决方法来解决任何标题在其单词之间有空格的问题。

<section id="theSection" title="The&#32;section">The section</section>

<script>
 console.log(document.getElementById('theSection').title.split(' ').join('&#32;'));
</script>
 

我知道这是愚蠢的回答, 但如果真的打算处理空间 unicode 然后点击具有此 <section id="theSection" title="The&#32;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/

相关文章:

javascript - React 应用程序具有暂存环境并在 AWS Elastic BeanStalk 上使用 Docker 进行部署

javascript - 在 JSON 文件中包含 HTML 标签

html - 如何使水平和垂直滚动出现在浏览器中?溢出 : auto; doesn't work

jquery - URL 未与服务器交互

javascript - 使用javascript而不是jquery获取div的所有内容,包括iframe的内容

javascript - 为什么正则表达式使用 Angular $sce 在 "t"中查找 "Georgia"?

javascript - Google Chart API 折线图上的日期错误

Javascript window.location.href 与index.php或根名称比较

java - 安卓,org.w3c.dom : No validating DocumentBuilder implementation available

javascript - DOM 中的缓存对象与 javascript 变量