私はHTMLの単純なページを持っていますが、divをトグルしていますが、すべてうまくいきますが、私のコードでif- :javacriptの短い手書きの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';
が、あまりにも長い間、おそらく乾燥させることができる、まだ感じのthats、うまく
、私は2つのアプローチを持っていますが、それぞれが正しい方法ではありません。
// 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 <<< これはどうか考えてください。 ありがとうございます..
この短いのif-else文を使用しての正しい方法である「」|| info.style.display ===" JavaScirptで虚偽。 'info.style.display = info.style.display === '' || info.style.display == 'none'? 'インラインブロック': 'なし'; ' – Tushar
'info.style'や' info.style.display'も一時変数に格納してください – Bergi