jQuery 找到自我

发布于 2024-10-22 10:32:00 字数 900 浏览 1 评论 0原文

这可能听起来很奇怪,但我正在开发一个需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一曲爱恨情仇 2024-10-29 10:32:00

使用 find('*') 的方法会消耗更多的 CPU 资源,我建议:

$('.block').find(selector).add($('.block').filter(selector));

approach with find('*') would be much more CPU intensive and I would recommend:

$('.block').find(selector).add($('.block').filter(selector));
本王不退位尔等都是臣 2024-10-29 10:32:00

我也遇到过这个问题。我像这样解决了它(基本上是 Romans Malinovskis 作为 jquery 插件的解决方案):

$.fn.findAll = function(selector) {
    return this.find(selector).add(this.filter(selector));
};

I came across this problem too. I solved it like this (basically Romans Malinovskis solution as a jquery plugin):

$.fn.findAll = function(selector) {
    return this.find(selector).add(this.filter(selector));
};
囍笑 2024-10-29 10:32:00

已编辑:

您可以将所有选择器“*”与 andSelf 结合使用来获取包含元素及其所有子子元素的选择。然后您可以在所选选择器上过滤()该选择。

<style type="text/css">li {background-color: white;}</style>
<script type="text/javascript">
$(function () {
  $('div').find('*').andSelf().filter(selector).css('background-color','blue');
}
</script>
<div>
This is a test
  <ul>
    <li class="test">This is a test</li>
    <li>This is a test</li>
    <li class="test">This is a test</li>
    <li>This is a test</li>
    <li class="test">This is a test</li>
    <li>This is a test</li>
  </ul>
</div>

如果合适的话,应该更改所有 .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.

<style type="text/css">li {background-color: white;}</style>
<script type="text/javascript">
$(function () {
  $('div').find('*').andSelf().filter(selector).css('background-color','blue');
}
</script>
<div>
This is a test
  <ul>
    <li class="test">This is a test</li>
    <li>This is a test</li>
    <li class="test">This is a test</li>
    <li>This is a test</li>
    <li class="test">This is a test</li>
    <li>This is a test</li>
  </ul>
</div>

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);

小嗲 2024-10-29 10:32:00

从 1.8 开始你可以做

$('.block').find(selector).addBack(selector);

Since 1.8 you can do

$('.block').find(selector).addBack(selector);
故事与诗 2024-10-29 10:32:00

我不知道为什么这需要如此复杂。这一切都可以通过一个简单的多重选择器来完成:

$(selector + '.block, .block ' + selector);

没有find过滤器,也不添加来考虑,加上它的在许多(大多数?)现代浏览器中性能更高(使用 jQuery 1.9 进行测试)。

注意事项

  • 您可能需要事先修剪用户提供的选择器,因为尾随空格在这里有意义;
  • 该解决方案在某种程度上特定于OP的问题,如果目标div的选择器以元素名称开头,即selector + 'th, th ' + 选择器不会产生有效的选择器,则该解决方案将不起作用;在这种情况下,我建议使用一种更通用的解决方案;
  • 您仍然可以坚持使用多选择器方法,使用一种极其 HACKY、完全不推荐的方法,该方法涉及使用 :not() 伪选择器: 查看工作演示可怕的,可怕的表现

I'm not sure why this needs to be so complicated. This can all be done with a simple multiple selector:

$(selector + '.block, .block ' + selector);

No find, filter, nor add to think about, plus it's more performant in many (most?) modern browsers (tested with jQuery 1.9).

Caveats:

  • You may want to trim the user-supplied selector beforehand, since trailing spaces would have meaning here;
  • This solution is somewhat specific to the OP's question and would not work if the target div's selector began with an element name, i.e. selector + 'th, th ' + selector would not yield a valid selector; in such cases I would recommend using one of the more generic solutions;
  • You could still stick to the multiple-selector approach with an EXTREMELY HACKY, NOT AT ALL RECOMMENDED approach that involves using the :not() pseudoselector: see working demo and horrible, horrible performance.
请你别敷衍 2024-10-29 10:32:00

我创建 jquery 方法 findAll 如下所示,

$.fn.findAll = function ( selector ) {
    if ( this.length === 0 ) {
        return this;
    } else if ( this.length === 1 ) {
        return this.filter( selector ).add( this.find( selector ) );
    } else {
        var rtn = this.filter( selector );
        this.each( function () {
            rtn.add( $( this ).find( selector ) );
        } );
        return rtn;
    }
}

您可以像下面一样使用它

$resultSet.findAll(selector)

I create jquery method findAll like below

$.fn.findAll = function ( selector ) {
    if ( this.length === 0 ) {
        return this;
    } else if ( this.length === 1 ) {
        return this.filter( selector ).add( this.find( selector ) );
    } else {
        var rtn = this.filter( selector );
        this.each( function () {
            rtn.add( $( this ).find( selector ) );
        } );
        return rtn;
    }
}

you can use it like below

$resultSet.findAll(selector)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文