如何使用 OR 条件链接选择器(如果 main 为空,则为替代结果集)

发布于 2024-12-12 01:04:00 字数 354 浏览 0 评论 0原文

我现在拥有的:

var result = $('selector1');
if (result.length == 0) result = $('selector2');

但这击败了链接。

问题是 - 如何通过 JQuery 链接获得相同的结果?

我无法使用 $('selector1, selector2'),因为这总是会为两个选择器选择结果集,而只有在没有选择器的情况下我才需要 selector2 的结果selector1 的匹配元素。

What I have now:

var result = $('selector1');
if (result.length == 0) result = $('selector2');

but this defeats chaining.

Question is - how do I get same result with JQuery chaining?

I can't use $('selector1, selector2'), because this would always select result sets for both selectors, while I need results of selector2 only if there are no matching elements for selector1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

倒带 2024-12-19 01:04:00

这种行为在某些地方称为“合并”。这是一个通用的 jQuery 插件,可以为您完成此操作(在大量反馈后进行编辑,请参阅评论)。

// The namespace function
jQuery.coalesce = function(selectors){

    var out;

    $.each(selectors, function(i, v){
        var el = jQuery(v);
        if (el.length) {
           out = el;
           return false;
        }
    });

    return out || jQuery();
};

// The jQuery plugin
jQuery.fn.coalesce = function(){
    return jQuery.coalesce(this.selector.split(",")); //a little brittle
};

因此,在 #foo 不存在,而 adiv 存在的世界中,如果您存在:

jQuery.coalesce(["#foo", "a", "div"])

则会返回 jQuery ("a") 如果 #foo 不存在,或者 jQuery("#foo") 如果 #foo 存在存在。

如果您需要在链的中间使用它,则可以使用 $("#foo, a, div").coalesce(),但它容易受到选择器本身内的命令的影响。

This behavior in some places is called "coalescing". Here's a generic jQuery plugin that does it for you (editing after great feedback, see the comments).

// The namespace function
jQuery.coalesce = function(selectors){

    var out;

    $.each(selectors, function(i, v){
        var el = jQuery(v);
        if (el.length) {
           out = el;
           return false;
        }
    });

    return out || jQuery();
};

// The jQuery plugin
jQuery.fn.coalesce = function(){
    return jQuery.coalesce(this.selector.split(",")); //a little brittle
};

So, in a world where #foo doesn't exist, and a and div do, if you do:

jQuery.coalesce(["#foo", "a", "div"])

That returns jQuery("a") if #foo doesn't exist, or jQuery("#foo") if #foo does exist.

If you require using it in the middle of the chain, you can use $("#foo, a, div").coalesce(), but its vulnerable to commans within the selectors themselves.

逆夏时光 2024-12-19 01:04:00

如果你必须,这可以做到:

$(function(){
  var r = $('selector1');
  if (r.length == 0) r = $('selector2');
  return r;
}());

但是,我不认为链接在你的例子中被击败。链接并不是万能的,加入像上面这样的痛苦选择更多的是使代码变得混乱而不是简化。

If you must, this would do it:

$(function(){
  var r = $('selector1');
  if (r.length == 0) r = $('selector2');
  return r;
}());

However, I don't think chaining is being defeated in your example. Chaining is not the be-all-end-all and putting in a tortured selection like the one above serves more to obfuscate the code than simplify.

み零 2024-12-19 01:04:00
var result = $('selector1');
( result.length ? result : $('selector2') ).css('color', 'red');
var result = $('selector1');
( result.length ? result : $('selector2') ).css('color', 'red');
笑咖 2024-12-19 01:04:00

我不相信您可以仅使用纯 jQuery 完全按照您想要的方式执行此操作,但是您可以通过使用 jQuery 的扩展选择器来获得您正在寻找的一些功能,该选择器允许您基于否定和布尔运算符进行选择。查看 http://api.jquery.com/category/selectors/jquery-选择器扩展/

I don't believe you can do this exactly how you want with just pure jQuery but you may be able to get some of the functionality you are looking for by using jQuery's extended selectors, which allow you to select based on negation and boolean operators. Check out http://api.jquery.com/category/selectors/jquery-selector-extensions/.

绮烟 2024-12-19 01:04:00

就我个人而言,我非常喜欢 Ruby on Rails 中的“存在”习惯用法,因此我已将其添加到我的 JS 设置中:

jQuery.fn.presence = function presence() {
    return this.length !== 0 ? this : null;
};

现在在我的代码中使用它:(

const ul = el.parents('UL').first().presence() ?? el.siblings('UL').first()

如果您无法负担使用 ??, || 在这里也可以。)

Personally I like very much the "presence" idiom from Ruby on Rails, so I have added this to my JS set up:

jQuery.fn.presence = function presence() {
    return this.length !== 0 ? this : null;
};

and now use this in my code:

const ul = el.parents('UL').first().presence() ?? el.siblings('UL').first()

(If you can't afford to use ??, || would do just as well here.)

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