Javascript 如何在 jQuery 中进行 if-then 速记?这些可以链接吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10140403/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:02:38  来源:igfitidea点击:

How to do a shorthand if-then in jQuery? Are these chainable?

javascriptjquerychaining

提问by frequent

Actually two questions:

其实两个问题:

I like the shorthand IF-ELSEin jQuerylike so:

我喜欢jQuery 中的速记IF-ELSE,如下所示:

var some == true ? "it's true" : "it's false";   

Is there a corresponding IF-THEN, because this:

是否有相应的IF-THEN,因为:

   var some == true ? "it's true" 

doesn't seem to work.

似乎不起作用。

Also, would something like this be chainable in regular jQuery? Like so:

另外,像这样的东西可以在常规 jQuery 中链接吗?像这样:

  some.addClass("hello")
      .find(".other").css("width","100px").end()
      // chained if
      .hasClass("one") ? some.css("border","1px solid red")
      .removeClass("hello")

Now that would be nice, wouldn't it? Is this possible?

现在那会很好,不是吗?这可能吗?

采纳答案by ipr101

There are a couple of plug-ins that allow you to chain iflogic in jQuery -

有几个插件可以让你if在 jQuery 中链接逻辑 -

http://outwestmedia.com/jquery-plugins/conditional/

http://outwestmedia.com/jquery-plugins/conditional/

$('div')
    .If('hasClass', 'myClass')
        .css('color', 'red')
    .Else('hasClass', 'anotherClass')
        .css('color', 'green')
    .Else()
        .css('color', 'blue');

http://benalman.com/projects/jquery-iff-plugin/

http://benalman.com/projects/jquery-iff-plugin/

function my_test( x ) {
  return x === 'bar';
};

$('div')
  .append( '1' )
  .iff( my_test, 'foo' )
    .append( '2' )
    .end()
  .append( '3' );

回答by GillesC

That will do (assignment uses a single =, two is for conditions), but if the first one is true, like you posted, some will ALWAYS be true.

那会做(赋值使用单个 =,两个用于条件),但如果第一个是真的,就像你发布的那样,有些永远是真的。

var some = true || "it's true"; 

And no, your example wouldn't be chainable, but you could replace your hasClass by filter('.one')which will continue the chain if there are elements containing the class one:

不,您的示例将无法链接,但是您可以替换您的 hasClass,filter('.one')如果有包含第一个类的元素,它将继续链:

some.addClass("hello")
      .find(".other").css("width","100px").end()
      .filter('.one').css("border","1px solid red")
      .removeClass("hello")

回答by adeneo

The shorthand IF/ELSE you are reffering to is called a ternary operator, and it's not chainable in the same way as a IF/ELSE statements, nor is it chainable as a jQuery method, and there are some limits to it's use, you can however place one inside the other, like so:

您所指的速记 IF/ELSE 称为三元运算符,它不像 IF/ELSE 语句那样可链接,也不能作为 jQuery 方法链接,并且它的使用有一些限制,您可以但是将一个放在另一个里面,如下所示:

some == true ? someMore == true ? "it's true" : "it's false" : "it's false";

You can also do:

你也可以这样做:

some == true ? "it's true" : null;
//or
some == true ? "it's true" : undefined;

This returns something, so usually it's used like so:

这会返回一些东西,所以通常它是这样使用的:

var some = someVar==true ? "it's true" : "it's false";

In other words you can do:

换句话说,你可以这样做:

var some = $(some).length ? 
             $(some).is('.one') ?
               some //some exists and have the class .one
             :
               return someOtherVar or function //some does not have the class .one
           :
             undefined //some does not have a length
           ;

You could also do something similar to this, and there are many different ways to use a ternary:

你也可以做类似的事情,有很多不同的方法可以使用三元:

$(div)[somevar==true ? fadeIn : fadeOut](300);

Other than that, when having a lot of stuff to perform a if/else is usually more appropriate, and if doing a lot of if/else/elseif checking, a switch/case is probably better.

除此之外,当有很多东西要执行 if/else 通常更合适,如果进行大量 if/else/elseif 检查,switch/case 可能更好。