Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

javascript - 如何获得$(this)选择器的子节点?(How to get the children of the $(this) selector?)

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.(并且想使用jQuery选择器在img选择div的子img 。) To get the div , I've got this selector:(为了得到div ,我有这个选择器:) $(this) How can I get the child img using a selector?(如何使用选择器获取子img ?)   ask by Alex translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The jQuery constructor accepts a 2nd parameter called context which can be used to override the context of the selection.(jQuery构造函数接受名为context的第二个参数,该参数可用于覆盖选择的上下文。)

jQuery("img", this); Which is the same as using .find() like this:(这与使用.find()一样的:) jQuery(this).find("img"); If the imgs you desire are only direct descendants of the clicked element, you can also use .children() :(如果你想要的imgs 只是被点击元素的直接后代,你也可以使用.children() :) jQuery(this).children("img");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...