jQuery 找到自我
这可能听起来很奇怪,但我正在开发一个需要在 div 或 div 本身中查找元素的插件。
该脚本根据用户选择查找元素,但内容(包括标记)是可变的。因此,脚本将按如下方式查找元素:
$('.block').find(selector); // selector set by user
但是没有一种简单的方法可以让选择器选择“.block”。 在使用 find 之前选择父级并不是一个解决方案,因为有多个 '.块'元素。
我知道扩展 expr[":"]
选择器不起作用,因为它只查找子项。但是,我确实找到了一种“鸭子冲床”这种方法,通过创建一个 ':self' 选择器:
(function($){
var orig = $.fn.find;
$.fn.find = function(sel){
return (sel === ':self') ? this : orig.call(this,sel);
}
})(jQuery)
但这似乎有点过头了。对于每个查找函数,它都会稍微减慢 jQuery 的处理速度。还有其他方法可以做到这一点吗?
感谢您的回答!但我最终这样做了:
var b = $('.block'),
el = (b.is(selector)) ? b : b.find(selector);
This may sound odd, but I'm working on a plugin that needs to find elements within a div, or the div itself.
The script finds an element based on a user selection, but the contents, including the markup is variable. So the script will look for the element as follows:
$('.block').find(selector); // selector set by user
but there isn't an easy way to have the selector select the '.block'. Selecting the parent before using find isn't a solution, as there are multiple '.block' elements.
I know extending the expr[":"]
selector won't work as it is only looking for children. But, I did figure out a way to "duck punch" this method, by making a ':self' selector:
(function($){
var orig = $.fn.find;
$.fn.find = function(sel){
return (sel === ':self') ? this : orig.call(this,sel);
}
})(jQuery)
But this seems a bit over the top. And it will slow jQuery processing a tiny bit with every find function. Is there another way to do this?
Thanks for the answers! But I ended up doing this:
var b = $('.block'),
el = (b.is(selector)) ? b : b.find(selector);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 find('*') 的方法会消耗更多的 CPU 资源,我建议:
approach with find('*') would be much more CPU intensive and I would recommend:
我也遇到过这个问题。我像这样解决了它(基本上是 Romans Malinovskis 作为 jquery 插件的解决方案):
I came across this problem too. I solved it like this (basically Romans Malinovskis solution as a jquery plugin):
已编辑:
您可以将所有选择器“*”与 andSelf 结合使用来获取包含元素及其所有子子元素的选择。然后您可以在所选选择器上过滤()该选择。
如果合适的话,应该更改所有 .test 对象的背景以及所选的初始 div。但我不确定我的答案的表现。
示例: http://jsfiddle.net/7A9JJ/2/
编辑
或者你可以只做 $('div, div *').filter(selector);
EDITED:
You can use the all selector '*' combined with andSelf to get a selection containing an element with all its children and subchildren. Then you can filter() that selection on the chosen selector.
Should change the backgrounds of all the .test objects, as well as the initial div that was selected, if it's appropriate. I'm not sure about the performance of my answer though.
Example: http://jsfiddle.net/7A9JJ/2/
EDIT
Or you could just do $('div, div *').filter(selector);
从 1.8 开始你可以做
Since 1.8 you can do
我不知道为什么这需要如此复杂。这一切都可以通过一个简单的多重选择器来完成:
没有
find
,过滤器
,也不添加
来考虑,加上它的在许多(大多数?)现代浏览器中性能更高(使用 jQuery 1.9 进行测试)。注意事项:
选择器
,因为尾随空格在这里有意义;selector + 'th, th ' + 选择器
不会产生有效的选择器,则该解决方案将不起作用;在这种情况下,我建议使用一种更通用的解决方案;:not()
伪选择器: 查看工作演示 和 可怕的,可怕的表现。I'm not sure why this needs to be so complicated. This can all be done with a simple multiple selector:
No
find
,filter
, noradd
to think about, plus it's more performant in many (most?) modern browsers (tested with jQuery 1.9).Caveats:
selector
beforehand, since trailing spaces would have meaning here;selector + 'th, th ' + selector
would not yield a valid selector; in such cases I would recommend using one of the more generic solutions;:not()
pseudoselector: see working demo and horrible, horrible performance.我创建 jquery 方法 findAll 如下所示,
您可以像下面一样使用它
I create jquery method findAll like below
you can use it like below