有人可以解释这个陈述的含义

时间:2011-12-09 14:39:25

标签: jquery height

我以前遇到过这种运算符的使用,我知道它是一个压缩的if语句,但是有人可以用外行的方式告诉我这意味着什么。

$("#portfolio-id").height($("#portfolio-id").height() > h-72 ? divHeights-72 : h-72);

我甚至不知道谷歌是什么叫它。 有没有人有我可以去的链接并自己找到答案?

非常感谢您的帮助

10 个答案:

答案 0 :(得分:4)

var newHeight;
if ($("#portfolio-id").height() > h-72)
{
  newHeight = divHeights-72;
}
else
{
  newHeight = h - 72;
}
$("#portfolio-id").height(newHeight);

未知h是什么或divHeights

答案 1 :(得分:1)

这意味着:

set the height of the element with id portfolio-id to the following value:
    if the height of the element with id portfolio-id is greater than h-72 then
        the value is divHeights-72
    else
        the value is h-72

此运算符通常称为三元运算符,通用格式为

testThisCondition ? returnThisIfTrue : returnThisIfFalse;

答案 2 :(得分:1)

使用id="portfolio-id"设置某些内容的新高度。如果它大于h-72,则将其设置为divheights-72;其他,将其设置为h-72

它被称为三元陈述

答案 3 :(得分:1)

如果#portfolio-id的高度大于h -72,则将投资组合高度设置为divHeights -72,否则将其设置为h-72

这是msdn文章解释三元运算符

http://msdn.microsoft.com/en-us/library/be21c7hw%28v=vs.94%29.aspx

这是另一个来自about.com

http://javascript.about.com/od/byexample/a/ternary-example.htm

答案 4 :(得分:1)

这是三元运营商。它就像

一样

var x = condition ? valueIfTrue : valueIfFalse;

这是编写if / else语句的基本简写。

答案 5 :(得分:1)

divHeights h 是声明的一些变量。声明正在使用三元if else,以确定#portfolio-id的新高度是什么。

//variable declarations
divHeights = x;
h = y;

//$("#portfolio-id").height() > h-72 ? divHeights-72 : h-72 is equivalent to the below code
if($("#portfolio-id").height() > h-72){
     newHeight = divHeights-72;
} else {
      newHeight =  h-72;
}

$("#portfolio-id").height(newHeight);

答案 6 :(得分:0)

$("#portfolio-id").height(              // sets #portfolio-id's height to...
   $("#portfolio-id").height() > h-72 ? //    if its current height is greater than h-72...
       divHeights-72                    //       then divHeights-72
     : h-72                             //       else h-72
);

答案 7 :(得分:0)

h中必须有某处值。

假设你的h = 100;

现在你正在检查“#portfolio-id”对象的高度。

if($(“#portfolio-id”)。height()> h-72) 比分配$(“#portfolio-id”)。height = divHeights-72 其他 assign $(“#portfolio-id”)。height = divHeights-72 h-72

答案 8 :(得分:0)

$("#portfolio-id").height($("#portfolio-id").height() > h-72 ? divHeights-72 : h-72);

|               boolean check                               |  | if true|    |if false|            

答案 9 :(得分:0)

它被称为Conditional Operator(或三元运算符)

该代码正在做的是:

var currentHeight = $("#portfolio-id").height(), newHeight = currentHeight; 
if (currentHeight > h-72) {            // if current height is greater than h - 72, use divHeights - 72
    newHeight = divHeights - 72;
} else {                               // otherwise use h - 72
    newHeight = h - 72;
}
$("#portfolio-id").height(newHeight);

它应该做什么(imo)是:

$("#portfolio-id").height(function(i, height) {
    if (height > h - 72) {
        return divHeights - 72;
    } else { 
        return h - 72;
    }
});

这使用了height function

的第二个版本