如何使用javascript获得HTML评论 [英] How do I get an HTML comment with javascript
问题描述
如果我有
<! - 一些评论 - >
如何获取此元素并使用javascript更改内容?
如果我有一个代码在里面,我想删除评论的标签,我该怎么做?
< h2>使用NodeIterator(IE> = 9)
最好的方法是使用专门的 NodeIterator 实例迭代给定根元素中包含的所有注释。
$ b
function filterNone(){
返回NodeFilter.FILTER_ACCEPT;
}
函数getAllComments(rootElem){
var comments = [];
//第四个参数,根据DOM4标准,它实际上已经过时,在IE 11
中需要var iterator = document.createNodeIterator(rootElem,NodeFilter.SHOW_COMMENT,filterNone,false);
var curNode;
while(curNode = iterator.nextNode()){
comments.push(curNode.nodeValue);
}
返回评论;
$ b $ window.addEventListener(load,function(){
console.log(getAllComments(document.body));
});
使用自定义的DOM遍历(也支持IE< 9)
如果您必须支持旧浏览器(例如IE< 9),则需要自己遍历DOM并提取那些节点类型是 Node.COMMENT_NODE
。
//感谢Yoshi的提示!
// Polyfill for IE< 9
if(!Node){
var Node = {};
if(!Node.COMMENT_NODE){
//根据DOM规范的数值
Node.COMMENT_NODE = 8;
}
函数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]);
}
}
返回评论;
}
提取节点的内容并将其删除
独立于您从上面选择的方式,您会收到相同节点的DOM对象。
访问注释内容非常简单,只需 commentObject.nodeValue
。
删除评论有点冗长: commentObject.parentNode.removeChild(commentObject)
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?
Using a NodeIterator (IE >= 9)
The best method is to use a dedicated NodeIterator instance iterating all comments contained in a given root element.
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)
If you have to support older browsers (e.g. IE <9), you need to traverse the DOM yourself and extract those elements whose node type is Node.COMMENT_NODE
.
// 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.
Accessing a comment's contents is as easy as commentObject.nodeValue
.
Deleting a comment is a bit more verbose: commentObject.parentNode.removeChild(commentObject)
这篇关于如何使用javascript获得HTML评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!