我有一个带有img的h4,就像这样。我将单击函数绑定到h4。这样做效果很好。但我无法选择其中的img。我想选择img以便用.attr(" src ").replace("up","down");来复制src属性。
<h4 class="collapsable_head">
<img id="up_down" class="icon" src="/crm/img/modifier_down.gif" alt="link"/>
<b>Classification Filter:</b>
</h4>javascript:
$(".collapsable_head").click(function(){
$(this).next(".collapsable_body").slideToggle(500)
//None of the next lines return me the img object
src = jQuery(this).children('img').attr("src");
print(src);
src = $(this).next("#up_down").attr("src");
print(src);
src = $(this).next("#up_down").attr("src");
print(src);
return false;
});我想使用关键字"this",因为我有更多的(“.collapsable_head”) ;-)
发布于 2010-01-08 04:42:51
var img = $("img", this); // Gets the image within 'this'第二个参数是选择器的上下文。在完整代码中,它看起来像这样:
$(".collapsable_head").click(function(){
var img = $("img:first", this).attr("src");
alert(img);
});发布于 2010-01-08 04:46:08
你有没有尝试:
jQuery(this).find('img').attr('src')应该和Jonathans的答案一样。
发布于 2015-05-20 17:48:29
$(function(){
$("img").on('error', function() {
$(this).attr('src',"/Files/446/178/themeblue/images/nopic.jpg");
});
}); https://stackoverflow.com/questions/2023453
复制相似问题