如何使用 javascript 获取 HTML 注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13363946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:33:21  来源:igfitidea点击:

How do I get an HTML comment with javascript

javascripthtml

提问by user1801625

If I have that

如果我有那个

  <!-- some comment -->

How do I get this element and change the content with javascript? And if I have a code inside that and I want to delete the tag of comments how can I do it?

如何获取此元素并使用 javascript 更改内容?如果我在里面有一个代码并且我想删除评论标签,我该怎么做?

回答by ComFreek

Using a NodeIterator (IE >= 9)

使用 NodeIterator (IE >= 9)

The best method is to use a dedicated NodeIteratorinstance iterating all comments contained in a given root element.

最好的方法是使用专用的NodeIterator实例迭代包含在给定根元素中的所有注释。

See it in action!

看到它在行动!

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;
}

window.addEventListener("load", function() {
    console.log(getAllComments(document.body));
});

Using a custom-made DOM traversal (to support IE < 9 as well)

使用定制的 DOM 遍历(也支持 IE < 9)

If you have to support older browsers (e.g. IE <9), you need to traverse the DOM yourself and extract those elements whose node typeis Node.COMMENT_NODE.

如果必须支持旧版浏览器(例如IE <9),则需要自己遍历DOM,提取节点类型Node.COMMENT_NODE.

See it in action!

看到它在行动!

// Thanks to Yoshi for the hint!
// Polyfill for IE < 9
if (!Node) {
    var Node = {};
}
if (!Node.COMMENT_NODE) {
    // numeric value according to the DOM spec
    Node.COMMENT_NODE = 8;
}

function getComments(elem) {
  var children = elem.childNodes;
  var comments = [];

  for (var i=0, len=children.length; i<len; i++) {
    if (children[i].nodeType == Node.COMMENT_NODE) {
      comments.push(children[i]);
    }
  }
  return comments;
}

Extracting a node's contents and delete it

提取节点的内容并删除它

Independent of the way you choose from above, you receive the same node DOM objects.

无论您从上面选择哪种方式,您都会收到相同的节点 DOM 对象。

Accessing a comment's contents is as easy as commentObject.nodeValue.
Deleting a comment is a bit more verbose: commentObject.parentNode.removeChild(commentObject)

访问评论的内容就像 一样简单commentObject.nodeValue
删除评论有点冗长:commentObject.parentNode.removeChild(commentObject)

回答by K..

You have to travers the DOM to get it. The nodeTypeof the comment DOM element is 8

你必须遍历 DOM 才能得到它。该nodeType评论DOM元素是8

if( oNode.nodeType === 8 ) {
  oNode.parentNode.removeChild( oNode );
}

would be an approach

将是一种方法

回答by Kna?is

Here is a JQuery plugin that retrieves the comments:

这是一个检索评论的 JQuery 插件:

http://www.bennadel.com/blog/1563-jQuery-Comments-Plug-in-To-Access-HTML-Comments-For-DOM-Templating.htm

http://www.bennadel.com/blog/1563-jQuery-Comments-Plug-in-To-Access-HTML-Comments-For-DOM-Templating.htm

The basic idea is to look at nodes, not elements:

基本思想是看nodes,而不是elements

http://www.w3schools.com/htmldom/dom_nodes.asp

http://www.w3schools.com/htmldom/dom_nodes.asp

You start with documentobject, and iterate through them using childNodescollection. You have to check for node.nodeType == 8which will return just the comment nodes (note that you need to iterate through child nodes recursively).

您从document对象开始,然后使用childNodes集合遍历它们。您必须检查node.nodeType == 8哪个将只返回评论节点(请注意,您需要递归遍历子节点)。