javascript jQuery 注释/取消注释 <!--element-->

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15553531/
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-27 01:10:52  来源:igfitidea点击:

jQuery comment/uncomment <!--element-->

javascriptjquerycommentselement

提问by George

I am looking for a way to wrap, with jQuery, an element into a comment, like:

我正在寻找一种使用 jQuery 将元素包装到注释中的方法,例如:

<!--
<div class="my_element"></div>
-->

and also a way to remove the comments.

也是一种删除评论的方法。

Is this possible?

这可能吗?

回答by smnh

To wrap an element with comment, or more specifically to replace an element with a comment node having that element's HTML:

用注释包装元素,或者更具体地说,用具有该元素的 HTML 的注释节点替换元素:

my_element_jq = $('.my_element');
comment = document.createComment(my_element_jq.get(0).outerHTML);
my_element_jq.replaceWith(comment);

To switch it back:

切换回来:

$(comment).replaceWith(comment.nodeValue);

If you don't have the reference to the comment node then you need to traverse the DOM tree and check nodeTypeof each node. If its value is 8 then it is a comment.

如果您没有对评论节点的引用,那么您需要遍历 DOM 树并检查nodeType每个节点。如果它的值为 8,那么它就是一个注释。

For example:

例如:

<div id="foo">
    <div>bar</div>
    <!-- <div>hello world!</div> -->
    <div>bar</div>
</div>

JavaScript:

JavaScript:

// .contents() returns the children of each element in the set of matched elements,
// including text and comment nodes.
$("#foo").contents().each(function(index, node) {
    if (node.nodeType == 8) {
        // node is a comment
        $(node).replaceWith(node.nodeValue);
    }
});

回答by Dom

You can comment the element out by doing the following:

您可以通过执行以下操作来注释该元素:

function comment(element){
    element.wrap(function() {
        return '<!--' + this.outerHTML + '"-->';
    });
}

DEMO: http://jsfiddle.net/dirtyd77/THBpD/27/

演示:http: //jsfiddle.net/dirtyd77/THBpD/27/

回答by Erik Campobadal

I'm impresed nobody gave the following solution. The following solution require a container. This container will have inside, the commented / uncommented code.

我印象深刻的是没有人给出以下解决方案。以下解决方案需要一个容器。这个容器里面有注释/未注释的代码。

function comment(element) {
    element.html('<!--' + element.html() + '-->')
}

function uncomment(element) {
    element.html(element.html().substring(4, element.html().length - 3))
}

function isCommented(element) {
    return element.html().substring(0, 4) == '<!--';
}

Example: https://jsfiddle.net/ConsoleTVs/r6bm5nhz/

示例:https: //jsfiddle.net/ConsoleTVs/r6bm5nhz/

回答by LiverpoolsNumber9

For wrapping?

为了包装?

function wrap(jQueryElement){
    jQueryElement.before("<!--").after("-->");
}

Not sure how successful you'd be finding the comments once wrapped though. A text search on the body element using regular expressions is an option.

不过,不确定一旦打包后您会发现评论有多成功。使用正则表达式对 body 元素进行文本搜索是一种选择。

Or this - is it possible to remove an html comment from dom using jquery

或者这个 -是否可以使用 jquery 从 dom 中删除 html 注释