jQuery 1.12.4 で、要素に data() を使って値を与えました。
console.log() で見る限り正しく値を持っているようですが、
実際に「data-」という属性が付けられているわけではないので
セレクタで [data-sample="〜"] と指定しても取得できません。
for と hasData() を使えば無理やり取得する事はできますが
このようなケースではどうやって特定のdataの値を持つ要素を選択するのでしょうか?
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jQuery</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script> $(function(){ $('#test li').first().data('sample', 'hello'); var elem = $('#test li[data-sample="hello"]'); console.log(elem.length); // 0 }); </script> </head> <body> <ul id="test"> <li>1</li> <li>2</li> <li>3</li> </ul> </body> </html>
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。