简体   繁体   English

使用jQuery选择HTML注释

[英]Selecting HTML comments with jQuery

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. 这不会发表评论。

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

There's the jQuery comments() plugin which will do that for you. jQuery comments()插件可以帮到你。 Usage: 用法:

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

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. 注意:我不确定,如果jQuery也返回注释。 If it does not, this approach does not work. 如果没有,这种方法不起作用。

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'. 每个广告都有唯一的“航班ID”。 Meaning the same 250x300 side rail ad can have multiple flights. 意思是相同的250x300侧轨广告可以有多个航班。 So in one impression, you might see an ad for Subway, refresh and perhaps Quizno's. 因此,在一次展示中,您可能会看到地铁,刷新和Quizno的广告。

Unfortunately, the service delivers this flight ID in a comment, and not as something a bit more useful like a data-attribute. 不幸的是,该服务在评论中提供此航班ID,而不是像数据属性那样有用的东西。 That said, each comment is within a tag. 也就是说,每条评论都在一个标签内。

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. 再次,在游戏后期道歉,但我认为提供另一种方法解决这个问题并没有什么坏处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM