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 に答える