简体   繁体   English

如何使用JavaScript捕获带注释的HTML?

[英]How to capture commented HTML with JavaScript?

I would like to know if it is possible to capture a commented remark using JavaScript. 我想知道是否可以使用JavaScript捕获注释的注释。

The remark will look like this on the soruce code: 这句话在soruce代码上看起来像这样:

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

I need to save that value (in this case 1107.45) inside a variable. 我需要在变量中保存该值(在本例中为1107.45)。

This doesn't seem to work: 这似乎不起作用:

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

Any ideas? 有任何想法吗?

Thanks! 谢谢!

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

Live DEMO 现场演示

And the full code: 和完整的代码:

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

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

Live DEMO 现场演示

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

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