我有一个简单的HTML页面有一个按钮切换div ,,但一切工作正常,但我有我的代码中的if-else语句像这样:不能在JavaScript干短手if-else(条件三元运算符)
if (info.style.display === '' || info.style.display == 'none'){
info.style.display = 'inline-block';
} else {
info.style.display = 'none';
}
我已经决定使用这样的简短语句;
info.style.display === ''||info.style.display ==='none' ? info.style.display = 'inline-block' :info.style.display = 'none';
,但仍然感觉这就是太长,大概可以干燥,
嗯,我有两种方法,但每个都是不正确的做法:
// this solution works but requires two clicks first time run:
info.style.display ==(''||'none') ?info.style.display = 'inline-block' :info.style.display = 'none';
和:
// this solution doesn't work:
info.style.display == (''||'none' ? 'inline-block' : 'none');
她是>>> My Plunker <<< 对此有任何想法吗? 谢谢..
'“” || info.style.display ==='的正确途径none''会经常检查第二部分为空字符串JavaScirpt中的虚假。 'info.style.display = info.style.display ===''|| info.style.display =='none'? 'inline-block':'none';' – Tushar
你应该尝试在一个临时变量中存储'info.style'或甚至'info.style.display' – Bergi