Javascript 使用jQuery复杂度获取属性
我试图获取另一个标记中的images alt属性,但问题是我到处都有类似的图像。我可以访问这些图像的唯一方法是使用Javascript 使用jQuery复杂度获取属性,javascript,jquery,Javascript,Jquery,我试图获取另一个标记中的images alt属性,但问题是我到处都有类似的图像。我可以访问这些图像的唯一方法是使用这个关键字 jQuery(".content p").mouseenter(function () { var temp = jQuery(this+' img').attr('alt'); jQuery(this).append("<span>shit men</span>"); }); jQuery(“.content p”).mouse
这个
关键字
jQuery(".content p").mouseenter(function () {
var temp = jQuery(this+' img').attr('alt');
jQuery(this).append("<span>shit men</span>");
});
jQuery(“.content p”).mouseenter(函数(){
var temp=jQuery(this+'img').attr('alt');
jQuery(this).append(“狗屎男人”);
});
HTML
通过使用
此
我只能访问
标签,但我想通过此
关键字访问
标签,而上述代码不起作用。我的问题是,是否可以将标记连接到此
例如此+'img'
?基本上,您需要在所选元素中获取img
,因此只需找到它即可
jQuery(".content p").mouseenter(function () {
var temp = jQuery(this).find('img').attr('alt');
jQuery(this).append("<span>shit men</span>");
});
jQuery(“.content p”).mouseenter(函数(){
var temp=jQuery(this.find('img').attr('alt');
jQuery(this).append(“狗屎男人”);
});
$('img',this).attr('alt')
adeneo->好吧,你应该把这个写在答案中,而不是评论中。但谢谢,它是有效的。请提供一个解释,而不仅仅是代码。很明显,OP是一个初学者,所以你解释得越多,你的答案就越有帮助。我是新来的,但感谢adaneo用简短的代码回答了问题。但答案上没有。
jQuery(".content p").mouseenter(function () {
var temp = jQuery(this).find('img').attr('alt');
jQuery(this).append("<span>shit men</span>");
});
$('img', this).attr('alt'); // source from adeneo