jQuery img选择 [英] jquery img select
本文介绍了jQuery img选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有img的h4,就像这样.我将点击功能绑定到h4.这很好.但是我无法选择其中的img.我想选择img,以便使用.attr("src").replace("up","down");替换src attr.
I have a h4 with an img in it like this. I bind a click function to the h4. This works well. But I'm not able to select the img in it. I want to select the img in order to replage the src attr with .attr("src").replace("up", "down"); .
<h4 class="collapsable_head">
<img id="up_down" class="icon" src="/crm/img/modifier_down.gif" alt="link"/>
<b>Classification Filter:</b>
</h4>
javascript:
The 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");-)
I want to use the keyword "this", since I have more (".collapsable_head")'s out there ;-)
推荐答案
var img = $("img", this); // Gets the image within 'this'
第二个参数是选择器的上下文.在完整代码中,它看起来像这样:
The second parameter is the context of the selector. In full-code, it looks like this:
$(".collapsable_head").click(function(){
var img = $("img:first", this).attr("src");
alert(img);
});
这篇关于jQuery img选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文