jQuery $(this +“选择器”) ? $("img",this) 可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10187280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$(this +"selector") ? $("img",this) possible?
提问by Plexus81
I'm trying to "select" a img inside a $(this) selector. I know I can find it by using .find('img')
but is this possible:
我正在尝试在 $(this) 选择器中“选择”一个 img。我知道我可以通过使用找到它,.find('img')
但这是否可能:
$("img",this)
?
$("img",this)
?
What's the most optimal way to do this?
执行此操作的最佳方法是什么?
Originally code
原码
<a class="picture" href="test.html">
<img src="picture.jpg" alt="awesome">
</a>
回答by Andreas Wong
What's the most optimal way to do this?
执行此操作的最佳方法是什么?
Both $(this).find('img')
and $('img', this)
are equivalent.
这两个$(this).find('img')
和$('img', this)
是等价的。
From the docs:
从文档:
Internally, selector context is implemented with the .find() method, so $('span', this) is equivalent to $(this).find('span').
在内部,选择器上下文是用 .find() 方法实现的,所以 $('span', this) 等价于 $(this).find('span')。
回答by Marco Johannesen
That a perfect reasonably way of doing it.
这是一种完美的合理方式。
So you would do like:
所以你会喜欢:
$('a').click( function() {
//if the element dosent change you can use this
//var src = $('img', this).attr('src');
//else use $(this)
var src = $('img', $(this)).attr('src');
alert(src);
return false;
});
See: http://jsfiddle.net/xYmwV/
There is really no difference, since in both methods you load the dom element, and search it. Your way ofcourse is "cleaner" and simpler, but might be more confusing :)
真的没有区别,因为在这两种方法中你都加载了 dom 元素并搜索它。你的方式当然是“更干净”和更简单,但可能更令人困惑:)
A faster way would be $(this).children()
since it then would'nt have to search for elements, but goes directly downwords in the DOM. But it takes out the flexibility of the script.
一种更快的方法是$(this).children()
因为它不必搜索元素,而是直接在 DOM 中搜索。但它消除了脚本的灵活性。
回答by mamoo
Yes you can do that... anyway they're equivalent, so it's only a matter of your 'syntactic tastes':
是的,你可以这样做......无论如何它们是等效的,所以这只是你的“语法品味”的问题:
Internally, selector context is implemented with the .find() method, so $('span', this) is equivalent to $(this).find('span').
在内部,选择器上下文是用 .find() 方法实现的,所以 $('span', this) 等价于 $(this).find('span')。
回答by T.J. Crowder
It doesn't really matter, choose whichever you prefer, it's primarily a stylistic choice.
这并不重要,选择您喜欢的任何一个,这主要是一种风格选择。
jQuery handles the form $(selector, context)
by doing $(context).find(selector)
or (if context
is already a jQuery instance) context.find(selector)
under the covers, so in theory the find
form is slightlymore efficient, but not in any way that's really likely to matter.
jQuery$(selector, context)
通过在幕后执行$(context).find(selector)
或(如果context
已经是 jQuery 实例)来处理表单context.find(selector)
,因此理论上该find
表单的效率稍高一些,但实际上并不重要。
回答by gabitzish
Why can't you use .find('img')
? it works to me : http://jsfiddle.net/nnmEY/
为什么不能用.find('img')
?它对我有用:http: //jsfiddle.net/nnmEY/
回答by NimChimpsky
I'm trying to "select" a img inside a $(this) selector.
我正在尝试在 $(this) 选择器中“选择”一个 img。
var myImg = $(this).find("img");