切换显示仅在第二次点击时工作 [英] toggle display only working on second click
本文介绍了切换显示仅在第二次点击时工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 div
,它是 display:none
,它应该在点击图标时出现。我的功能可以工作,但始终在 第二个 点击。任何想法有什么错误的功能?
I have a div
that is display:none
which should appear when an icon is clicked. My function works but always on the second click. Any ideas what's wrong with the function?
document.getElementById('icon').onclick = function(){
var el = document.getElementById('div');
if ( el.style.display != 'none' ){
el.style.display = 'none';
}
else {
el.style.display = 'block';
};
};
推荐答案
将您的测试改为正面
if ( el.style.display == 'block' ){
它可以工作。
默认值可能不是'none'。
The default is probably not exactly 'none'.
使用jQuery可以使这更容易,请参阅 http://api.jquery .com / toggle /
Using jQuery would make that a lot easier, see http://api.jquery.com/toggle/
这篇关于切换显示仅在第二次点击时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文