How to write ternary operator condition in jQuery?

后端 未结 9 1544
眼角桃花
眼角桃花 2020-12-29 19:41

In this fiddle http://jsfiddle.net/mjmitche/6nar4/3/, if you drag, for example, the little blue box into the yellow box, then the big black box will turn pink. All of the 4

9条回答
  •  情深已故
    2020-12-29 20:08

    I think Dan and Nicola have suitable corrected code, however you may not be clear on why the original code didn't work.

    What has been called here a "ternary operator" is called a conditional operator in ECMA-262 section 11.12. It has the form:

    LogicalORExpression ? AssignmentExpression : AssignmentExpression
    

    The LogicalORExpression is evaluated and the returned value converted to Boolean (just like an expression in an if condition). If it evaluates to true, then the first AssignmentExpression is evaluated and the returned value returned, otherwise the second is evaluated and returned.

    The error in the original code is the extra semi-colons that change the attempted conditional operator into a series of statements with syntax errors.

提交回复
热议问题