3

我想知道是否可以使用 JavaScript 捕获评论的评论。

源代码上的注释将如下所示:

<div id='ContainerId'>
<!-- NON IDEAL REASON: 90 min time window depart arrive -->
<b>1,107.45 GBP</b><br />
<!--LLF: 1107.45 -->
</div>

我需要将该值(在本例中为 1107.45)保存在变量中。

这似乎不起作用:

var LLF = jQuery("contains('LLF')");

有任何想法吗?

谢谢!

4

1 回答 1

5
$('#ContainerId').contents().filter(function(){
    return this.nodeType === 8 // Comment node
});

现场演示

以及完整的代码:

var comment = $('#ContainerId').contents().filter(function() {
    return this.nodeType === 8 // Comment node
})[0].nodeValue;

console.log(comment.match(/\d+\.\d+/g));​​​​​

现场演示

于 2012-05-25T14:46:42.867 回答