[英]false statement not executed by ternary operator
In the following code, the ternary operator isn't assigning the value if condition returns false! 在以下代码中,如果condition返回false,则三进制运算符不会分配该值!
<script>
function lgn() {
document.getElementById('overlay').style.display = 'none' ? 'block':'none';
document.getElementById('lgn').style.display = 'none' ? 'block':'none';
}
</script>
I am actually trying to make a overlay over my webpage, following is the Twig: 我实际上是想在我的网页上进行覆盖,以下是Twig:
<div class="nvg">
<ul>
{% block links %}
<li class="n.link"><span class="fa fa-times close" onclick="lgn()"></span></li>
{% endblock %}
</ul>
</div>
AND 和
{% block overlay %}
{{ form_start(form, {'attr':{'id':'lgn'}}) }}
<span class="fa fa-times close" onclick="lgn()"></span><br />
<span>Login:</span>
{{ form_widget(form.email, {'attr':{'placeholder':'email'}}) }}
{{ form_widget(form.pass, {'attr':{'placeholder':'password','class':'pass'}}) }}
{{ form_end(form) }}
{% endblock %}
Please help... 请帮忙...
The ternary makes a boolean decisions, and you've written yours to always evaluate to true: 三元决策做出布尔决策,并且您已经编写了始终决定为true的决策:
'none' ? 'block':'none';
is basically the same as 基本上和
if ('none' is true) {
'block'
} else {
'none';
}
Since none
as a string is a truthy (evaluates to the same as boolean true in most cases), you always pick the "true" path in the ternary. 由于
none
一个字符串是真实的(在大多数情况下,其评估结果与布尔true相同),因此您始终在三进制中选择“ true”路径。
Your ternary doesn't make sense. 你的三元没有意义。
document.getElementById('overlay').style.display = 'none' ? 'block':'none';
There's no logical evaluation to make, and the string will (mostly) always be evaluated to true. 无需进行逻辑评估,并且字符串(通常)始终会评估为true。 Do something like this:
做这样的事情:
document.getElementById('overlay').style.display = (document.getElementById('overlay').style.display == 'none') ? 'block' : 'none';
Your best solution (in terms of cleanliness) is to make the selectors into variables. 最好的解决方案(就清洁度而言)是使选择器成为变量。
function lgn() {
element1 = document.getElementById('overlay');
element1.style.display = (element1.style.display == 'none') ? 'block' : 'none';
element2 = document.getElementById('lgn');
element2.style.display = (element2.style.display == 'none') ? 'block' : 'none';
}
You always assign the display
to 'block'
because the 'none'
is a non-empty string and it is always true 您始终将
display
分配给'block'
因为'none'
是一个非空字符串 ,并且始终为 true
document.getElementById('overlay').style.display = 'none' ? 'block':'none';
Use this statement 使用此语句
var display = document.getElementById('overlay').style.display
document.getElementById('overlay').style.display = display == 'none' ? 'block':'none';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.