在 jQuery 中,我选择了一个 html 类,并使用 'this'
在函数中引用它。如何在该类中选择 img
?
下面的代码没有工作:
HTML:
<div class="test">
<img src="batman.png">
</div>
jQuery:
$(".test").mouseover(function() {
console.log($(this img).attr("src"));
});
最佳答案
您需要使用 $.fn.find()
方法
$(this).find('img')
或者,使用 jQuery(selector ,context)
, 在内部它会被转换为 find()
context: A DOM Element, Document, or jQuery to use as context
$('img', this)
关于jquery - 组合 'this' 选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30866235/