叼花硬汉 2008-11-20 19:44 采纳率: 0%
浏览 373
已采纳

如何获得 $(this)选择器的子级?

I have a layout similar to this:

<div id="..."><img src="..."></div>

and would like to use a jQuery selector to select the child img inside the div on click.

To get the div, I've got this selector:

$(this)

How can I get the child img using a selector?

转载于:https://stackoverflow.com/questions/306583/how-to-get-the-children-of-the-this-selector

  • 写回答

16条回答 默认 最新

  • 撒拉嘿哟木头 2008-11-20 21:27
    关注

    The jQuery constructor accepts a 2nd parameter called context which can be used to override the context of the selection.

    jQuery("img", this);
    

    Which is the same as using .find() like this:

    jQuery(this).find("img");
    

    If the imgs you desire are only direct descendants of the clicked element, you can also use .children():

    jQuery(this).children("img");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(15条)

报告相同问题?