我有以下内容:
<div class="mCSB_container mCS_no_scrollbar" style="position: relative; left: 0px; width: 195px;">
<li data-full-image="http://cf.test.com/images/store_logos/original/164df02513c4cfd3235c47a05f05c17005e178cb.jpg" data-large-image="http://cf.test.com/images/store_logos/original/164df02513c4cfd3235c47a05f05c17005e178cb.jpg" class="active">
<img src="http://cf.test.com/images/store_logos/thumbnail/164df02513c4cfd3235c47a05f05c17005e178cb.jpg" alt="" width="228" height="304">
</li>
<li data-full-image="http://cf.test.com/images/store_logos/original/2027539ce2d318d297bb3298f30a433d1b2bae4a.jpg" data-large-image="http://cf.test.com/images/store_logos/original/2027539ce2d318d297bb3298f30a433d1b2bae4a.jpg" class="">
<img src="http://cf.test.com/images/store_logos/thumbnail/2027539ce2d318d297bb3298f30a433d1b2bae4a.jpg" alt="" width="228" height="304">
</li>
</div>
我尝试过:
$('.mCSB_container').children('li').each(function () {
console.log(this.data("data-full-image"));
});
然后它说它里面没有方法数据。知道为什么吗?
答案 0 :(得分:3)
data
是一个jQuery方法,所以你需要在jQuery中包装this
,否则你只是引用了没有data
方法的DOM元素:
$(this).data("data-full-image")
答案 1 :(得分:1)
.data()
是一个jQuery方法,所以给this
一个jquery包装器:
$(this).data('full-image');
答案 2 :(得分:0)
将此包装在jQuery $(this)
中$('.mCSB_container').children('li').each(function () {
console.log($(this).data("full-image"));
});
答案 3 :(得分:0)
你可以
$('.mCSB_container').children('li').each(function () {
console.log($(this).data("fullImage"));
});
答案 4 :(得分:0)
你也可以这样做
$('.mCSB_container').children('li').each(function () {
console.log($(this).attr("data-full-image"));
});
答案 5 :(得分:0)
你可以
$(document).ready(function(){
alert( $('.mCSB_container li').data('full-image'))
})