抱歉 jQuery noob 问题,我正在尝试使用回调函数使所有具有类 .thumbnail 的 div 元素都可以单击。但是,单击该类的一个 div 后,我需要该给定 div 的特定 ID,以便我可以对该特定 div 进行进一步操作。我很困惑,如果我在点击它后使用“this”来引用该特定的 div,或者如果我以错误的方式看待它。
我确定这是一个非常简单的问题,让 jQuery 大师们来回答,这是漫长的一天,我的大脑完全僵化了。
Sudo 代码示例:
<script>
$(document).ready(function() {
$(".thumbnail").click(function() {
//need to get id of thumbnail that was clicked, this is where I am confused
var thumbnail_id = $(this).attr('id')
alert(thumbnail_id);
});
});
</script>
<div class=thumbnail" id="1">Tom</div>
<div class=thumbnail" id="2">Jerry</div>
<div class=thumbnail" id="3">Sue</div>
<div class=thumbnail" id="4">Mary</div>
<div class=thumbnail" id="5">Brian</div>
最佳答案
不要认为缩略图应该写成 "thumbnail"
而不是 thumbnail"
关于javascript - 将 jQuery 'this' 与多个元素的选择器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15712600/