JavaScript childNodes – 如何获取值?为什么控制台记录的值返回 undefined?

huangapple go评论62阅读模式
英文:

javascript childNodes - how to get values? whi a console.logged value return undefined?

问题

以下是翻译好的部分:

Ciao
我有一些类似这样的xHTML
<!DOCTYPE html>
  <html>
    // [ 在这里是有效的xHTML ]
  </html>
<!-- 这里有一个注释 -->

我想获取最后一个注释内的文本字符串:"a comment here"。
注意:它位于主HTML标签之外。

我可以尝试在控制台中记录它:
`var goal = document.getElementsByTagName('html')[0].parentNode.childNodes; console.log(goal);`

... goal.length 是 3,最终 goal[2].textContent 应该包含字符串 "a comment here",

但我已经尝试过使用 .nodeValue、innerHTML 等方法,还尝试过转换为数组,但都返回 undefined...
英文:

Ciao

i have some xHTML like this:


&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    // [ wellformed xHTML here ] 
  &lt;/html&gt;
&lt;!-- a comment here --&gt;`

I would get the text string inside the last comment: "a comment here".
Note: it's outside main html tag.

I can log it in console trying:
var goal = document.getElementsByTagName(&#39;html&#39;)[0].parentNode.childNodes; console.log(goal);

... goal.lenght is 3, and finally goal[2] .textContent should contain the string "a comment here",

but i tried yet with .nodeValue, innerHTML etc, also converting to array, and all return undefined...

答案1

得分: 0

以下是翻译好的部分:

Comments are anonymous, you cannot read them anywhere except your code editor, and that’s the whole point.

Aside from comments not being designed to be readable by JavaScript, you can still see them in their parent node’s innerHTML.

A simple way to do this is:

  • Get the parent HTML code
  • Parse it and get all of the comments
  • Return the value so you can use it

Here’s a simple script that will get all of the comments from the entire page:

function filterNone() {
    return NodeFilter.FILTER_ACCEPT;
}

function getAllComments(rootElem) {
  var comments = [];
  // Fourth argument, which is actually obsolete according to the DOM4 standard, is required in IE 11
  var iterator = document.createNodeIterator(rootElem, NodeFilter.SHOW_COMMENT, filterNone, false);
  var curNode;
  while (curNode = iterator.nextNode()) {
    comments.push(curNode.nodeValue);
  }
  return comments;
}

// Wait until the window is done loading
window.addEventListener(&quot;load&quot;, function() {
  console.log(getAllComments(document.body));
});
英文:

Comments are anonymous, you cannot read them anywhere except your code editor, and that’s the whole point.

Aside from comments not being designed to be readable by JavaScript, you can still see them in their parent node’s innerHTML.

A simple way to do this is:

  • Get the parent HTML code
  • Parse it and get all of the comments
  • Return the value so you can use it

Here’s a simple script that will get all of the comments from the entire page:

function filterNone() {
    return NodeFilter.FILTER_ACCEPT;
}

function getAllComments(rootElem) {
  var comments = [];
  // Fourth argument, which is actually obsolete according to the DOM4 standard, is required in IE 11
  var iterator = document.createNodeIterator(rootElem, NodeFilter.SHOW_COMMENT, filterNone, false);
  var curNode;
  while (curNode = iterator.nextNode()) {
    comments.push(curNode.nodeValue);
  }
  return comments;
}

// Wait until the window is done loading
window.addEventListener(&quot;load&quot;, function() {
  console.log(getAllComments(document.body));
});

huangapple
  • 本文由 发表于 2023年2月14日 22:01:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定