繁体   English   中英

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

[英]How to capture commented HTML with JavaScript?

我想知道是否可以使用JavaScript捕获注释的注释。

这句话在soruce代码上看起来像这样:

<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')");

有任何想法吗?

谢谢!

$('#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));​​​​​

现场演示

暂无
暂无

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

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