简体   繁体   中英

How to capture commented HTML with JavaScript?

I would like to know if it is possible to capture a commented remark using JavaScript.

The remark will look like this on the soruce code:

<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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