質問するログイン新規登録

質問編集履歴

2

内容を一部修正いたしました

2017/10/13 08:29

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -18,7 +18,9 @@
18
18
  stop: function () {
19
19
  $('#hoge').each(function () {
20
20
  var $elm = $(this).children('.items');
21
+ var $test = $(this).children('.items').data('aaa'); //追記部分
21
- console.log($elm);
22
+ console.log($elm); //追記:eachでなくとも取得できる
23
+ console.log($test); //追記:data-aaa="10"のみ取得できる
22
24
  });
23
25
  },
24
26
  });
@@ -27,10 +29,16 @@
27
29
 
28
30
  ```html
29
31
  <section id="hoge">
30
- <div class="items">あああ</div>
32
+ <div class="items" data-aaa="10">あああ</div>
31
- <div class="items">いいい</div>
33
+ <div class="items" data-aaa="20">いいい</div>
32
- <div class="items">ううう</div>
34
+ <div class="items" data-aaa="30">ううう</div>
33
- <div class="items">えええ</div>
35
+ <div class="items" data-aaa="40">えええ</div>
34
- <div class="items">おおお</div>
36
+ <div class="items" data-aaa="50">おおお</div>
35
37
  </section>
36
- ```
38
+ ```
39
+ ※data-aaaを追記いたしました。
40
+
41
+ ###追記
42
+ masaya_ohashi様のご指摘の通り、```$elm```の部分は```.each()```でなくとも取得できましたが、その下にある```.items```の```data-aaa```を取得しようとすると、はじめの行```<div class="items" data-aaa="10">あああ</div>```のみ取得できる形となってしまいます。
43
+
44
+ そうではなく、すべての```.items```の```data-aaa```を取得したい場合、どのようにすれば良いでしょうか。

1

html部分を追記いたしました

2017/10/13 08:29

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -23,4 +23,14 @@
23
23
  },
24
24
  });
25
25
  });
26
+ ```
27
+
28
+ ```html
29
+ <section id="hoge">
30
+ <div class="items">あああ</div>
31
+ <div class="items">いいい</div>
32
+ <div class="items">ううう</div>
33
+ <div class="items">えええ</div>
34
+ <div class="items">おおお</div>
35
+ </section>
26
36
  ```