<div class="myClass" data-info="35">
<div class="box-red"></div>
</div>
<div class="myClass" data-info="51">
<div class="box-blue"></div>
</div>
<div class="myClass" data-info="81">
<div class="box-black"></div>
</div>
[class^="box"]{
width: 40px;
height: 40px;
display: block;
}
.box-red{
background-color: red;
}
.box-blue{
background-color: blue;
}
.box-black{
background-color: black;
}
console.log($('.myClass'))
<div class="myClass" data-info="35">…</div>,
<div class="myClass" data-info="51">…</div>,
<div class="myClass" data-info="81">…</div>
当我console.log($('.myClass').data('info');)
时,我只得到35
我如何获得所有价值?我必须使用each
吗?而each
是唯一的方法吗?感谢
答案 0 :(得分:2)
使用$.each
$.each($('.myClass'),function(){
console.log($(this).data('info'));
});