简体   繁体   English

用于text()的jQuery条件语句(三元)

[英]jQuery Conditional Statement (Ternary) for text()

I'm trying to set the text value of an element (promo-footer) to the contents of a variable (footerVar) if it is not an empty string ''. 我正在尝试将元素(promo-footer)的文本值设置为变量(footerVar)的内容,如果它不是空字符串'。

$('.promo-footer').text(footerVar == '' ? 'no' : footerVar);

That works, and only displays the footer text if it exists, and the variable is an empty string '' then it displays "no"... 可行,并且仅显示页脚文本(如果存在),并且变量为空字符串”,则显示“否” ...

My question is - why does this work? 我的问题是-为什么这样做? I thought the first thing after the question mark happens if the equation evaluates to true ? 我以为如果方程式的计算结果为true问号之后就会发生第一件事?

x = (1 < 2) ? true : false;

Here it is in action: https://jsfiddle.net/xfzgaLq6/ 它正在起作用: https : //jsfiddle.net/xfzgaLq6/

This footerVar == '' to true when footerVar is an empty string. 如果footerVar为空字符串, footerVar footerVar == ''为true。 But in your case it is a non empty string. 但是在您的情况下,它是一个非空字符串。 So it evaluates to false and the expression belongs to false part got returned. 因此它的计算结果为false并且返回了属于false部分的表达式。 ie] after : 即]之后:

The following example would clarify your doubt on ternary operator usage. 以下示例将澄清您对三元运算符用法的疑问。

var x = (true) ? 10 : 20;
console.log(x); //10;

var x = (false) ? 10 : 20;
console.log(x); //20;

This is the syntax for ternary operator , 这是三元运算符的语法,

(condition) 
  ? expression has to be returned when condition evaluates to true
  : expression has to be returned when condition evaluates to false

You right, Because if the footerVar === ' ', then the condition is true.(footer is empty ) and it return the first statement. 您说对了,因为如果footerVar ==='',则条件为true。(footer为empty),它返回第一个语句。 and if footerVar is not empty then the condition is false and it return the 2nd statement. 如果footerVar不为空,则条件为false并返回第二条语句。

It works the way it should. 它按应有的方式工作。

var promotionObj = {};
promotionObj.footer_text = "foot test";

// This works, says "foot test".  Why??
$('.promo-footer').text(promotionObj.footer_text == '' ? 'no' : promotionObj.footer_text);

// This says "no":
$('.promo-footer').text(promotionObj.footer_text == '' ? promotionObj.footer_text : 'no');

Now considering the above code which is from the fiddle you posted. 现在考虑上面发布的小提琴中的代码。 The first one says "foo test" because promotionObj.footer_text is not an empty string. 第一个说“ foo测试”,因为promotionObj.footer_text不是空字符串。 The second part of the code says "no" because you interchanged the arrangement of the expression in which the value of the variable: promotionObj.footer_text will only be used as the footer text if it is empty and in this case it is not empty, therefore "no" will be displayed in its stead. 代码的第二部分说“ no”,因为您交换了表达式的排列方式,其中变量的值:promotionObj.footer_text仅在为空且在这种情况下不为空时才用作页脚文本,因此,将以“ no”代替。

Consider this. 考虑一下。

var arg = 5;
var result = arg > 10 ? arg : 0;  // result contains 0
var result = arg > 10 ? 0 : arg // result contains 5 which is the value of arg

I hope the explanation is clear. 我希望解释清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM