使用 jQuery 选择 HTML 注释

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

Selecting HTML comments with jQuery

jqueryhtml

提问by Anthony Johnston

Does any one know how to select HTML comment nodes with jQuery?

有谁知道如何使用 jQuery 选择 HTML 注释节点?

<html>
<head>
    <title>Check Test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $("body *").each(function() {
                alert($(this).wrap("<span />").parent().html());
            });
        });
    </script>
</head>
<body>
    <!-- Hello -->  
    <p>
        <label for="thing">Thing Label</label>
        <input id="thing" type="checkbox" />
    </p>

This doesn't pick up the comment.

这不接受评论。

采纳答案by karim79

There's the jQuery comments() pluginwhich will do that for you. Usage:

jQuery comments() 插件可以为您做到这一点。用法:

var comments = $( "#foo" ).comments();
alert(comments.html());

回答by Frank Geueke

This seems to do something equivalent to what you're looking for:

这似乎与您正在寻找的内容相同:

    $(function() {
        $("body").contents().filter(function(){
            return this.nodeType == 8;
        }).each(function(i, e){
            alert(e.nodeValue);
        });
    });

回答by Mike

And if you don't want a plugin:

如果你不想要插件:

var content = jQuery('body').html();
alert(content.match(/<!--.*?-->/g));

This uses regular expressions. It is set to search for anything enclosed by <!--and -->while it doesn't matter what's written on the inside.

这使用正则表达式。它被设置为搜索被包围的任何东西<!---->而里面写的是什么并不重要。

NOTE: I am not sure however, if jQuery also returns comments. If it does not, this approach does not work.

注意:但是,我不确定 jQuery 是否也返回注释。如果没有,则此方法不起作用。

回答by Timothy Petrakis

Once again, I'm late in the game, but I thought I'd contribute back as I found this post helpful in a recent situation I faced.

再一次,我迟到了,但我认为我会做出贡献,因为我发现这篇文章对我最近遇到的情况很有帮助。

In our context, we have an ad service delivering blocks of code to render the ad.

在我们的上下文中,我们有一个广告服务提供代码块来呈现广告。

Each ad has unique 'flight id'. Meaning the same 250x300 side rail ad can have multiple flights. So in one impression, you might see an ad for Subway, refresh and perhaps Quizno's.

每个广告都有唯一的“航班 ID”。这意味着同一个 250x300 侧栏广告可以有多个广告投放。因此,在一次印象中,您可能会看到 Subway、refresh 和 Quizno 的广告。

Unfortunately, the service delivers this flight ID in a comment, and not as something a bit more useful like a data-attribute. That said, each comment is within a tag.

不幸的是,该服务在评论中提供了此航班 ID,而不是像数据属性那样更有用的东西。也就是说,每个评论都在一个标签内。

From the above, I was able to put together this solution to obtain the flight number in the comment leveraging JavaScript's exec() method of the RegExp object:

从上面,我能够将这个解决方案放在一起,以利用 RegExp 对象的 JavaScript 的 exec() 方法获取注释中的航班号:

regexComment = new RegExp(/<!--\s*ad flight id:\s*([0-9]+)\s*-->/i);
targetElement = regexComment.exec($('div.advertisement').html());
if(targetElement.length > 0) {
    return parseInt(targetElement[1]);
}

Again, apologies for late in the game, but I thought it wouldn't hurt to provide yet another approach to this issue.

再次为游戏后期道歉,但我认为提供另一种方法来解决这个问题不会有什么坏处。