search

Home  >  Q&A  >  body text

JS中三元运算符 冒号: 后可以写多条语句吗?

当问号?前表达式不满足时,需要运行两条语句。请问需要冒号:后要怎么写?

1

2

3

4

5

let s = document.getElementById('mainContent').style;

s.opacity = 1;

(function fade(){

(s.opacity-=.1)<0?s.display="none":{setTimeout(fade,100);console.log(123);}

})();

这样写会报错。只能用if-else了吗?

城堡下的诡洞城堡下的诡洞2955 days ago1830

reply all(2)I'll reply

  • 数据分析师

    数据分析师2017-10-01 00:55:53

    Can multiple statements be written after the colon: in the ternary operator in JS? -PHP Chinese website Q&A-Can multiple statements be written after the ternary operator colon: in JS? -PHP Chinese website Q&A

    Let’s take a look and learn.

    reply
    0
  • 迷茫

    迷茫2017-03-22 09:29:24

    用自执行匿名函数

    1

    2

    3

    4

    5

    6

    7

    8

    var time = +new Date()

    var result = time % 2 === 0 ? 'ok' : (_ => {

      time = +new Date()

      console.warn(time)

      return 'no'

    })()

     

    console.info(result)

    reply
    0
  • Cancelreply