9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

要素からコメントアウトを取り出す

Last updated at Posted at 2014-11-19

この例ではbody直下にあるコメントの内容を一覧で出力します。

index.html
<!DOCTYPE html>
<html>
<head></head>
<body>
  <!--hoge-->
  foo
  <!--piyo-->
  bar
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="main.js"></script>
</body>
</html>
main.js
var $elm = $('body');
var $comment = $elm.contents().filter(function() {
    return this.nodeType === document.COMMENT_NODE;
});
$comment.each(function(i, elm) {
    console.log(elm.data);
});

[出力]
hoge
piyo

参考:
[jQuery]タグのnodeTypeで比較可能な値とその意味の一覧

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?