我有21个图像动态加载到多个div。下面是图像的一个例子。
<img src="fruits/lychee.png" class ="fruit" id="fruitl156">
<img src="fruits/cranberry.png" class ="fruit" id="fruitl141">
<img src="fruits/avocado.png" class ="fruit" id="fruitl214">当页面加载3-6个随机图像时,将可见。当单击$('#findVisible')时,我想获取每个图像的id。这是我的代码,但它没有通知id?如何获得每个可见图像的ID?
$('#findVisible').click(function(){
if ($('.fruit:visible').length > 0) {
//dosomething
$('.fruit:visible').each(function(){
var g = $(this.id);
alert(g) //DOES NOT WORK?
});
return false;
}
});发布于 2015-08-04 02:43:29
从$包装中删除$(this.id)包装
$('#findVisible').click(function() {
if ($('.fruit:visible').length > 0) {
//dosomething
$('.fruit:visible').each(function() {
var g = this.id;
alert(g) //DOES NOT WORK?
});
return false; //to stop refreshing the page
}
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img src="fruits/lychee.png" class="fruit" id="fruitl156">
<img src="fruits/cranberry.png" class="fruit" id="fruitl141">
<img src="fruits/avocado.png" class="fruit" id="fruitl214">
<button id="findVisible">click</button>
https://stackoverflow.com/questions/31799816
复制相似问题