[attribute]

$('[attribute]') 1.0追加

指定した属性を持つ要素を選択します。

項目 説明
attribute 属性名を指定します。

デモ

ID属性を持つ要素をクリックすると、IDの値がテキスト出力されます。

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div>ID無し</div>
<div id="hey">ID有り</div>
<div id="there">ID有り</div>
<div>ID無し</div>
<script>
  $('div[id]').one('click', function(){
    var idString = $(this).text() + ' = ' + $(this).attr('id');
    $(this).text(idString);
  });
</script>
</body>
</html>

 Back to top

© 2010 - 2017 STUDIO KINGDOM