jquery img选择
我有一个带有img的h4就像这样。 我将点击function绑定到h4。 这很好用。 但是我无法在其中选择img。 我想选择img以便用.attr(“src”)重放src attr.replace(“up”,“down”); 。
Classification Filter:
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”)在那里;-)
var img = $("img", this); // Gets the image within 'this'
第二个参数是选择器的上下文。 在完整代码中,它看起来像这样:
$(".collapsable_head").click(function(){ var img = $("img:first", this).attr("src"); alert(img); });
你试过了吗:
jQuery(this).find('img').attr('src')
应该像Jonathans的回答一样。
$(function(){ $("img").on('error', function() { $(this).attr('src',"/Files/446/178/themeblue/images/nopic.jpg"); }); });