JavaScript:如何隐藏/取消隐藏< div> [英] JavaScript: How to Hide / Unhide <div>
问题描述
我试图避免使用 innerHTML
,因为它会导致浏览器崩溃,这可能是由于刷新率为250毫秒。
无论如何,我宁愿在隐藏< div>
中添加一些内容,并使< div>
只有在满足某个条件时才可见。
基本上,我现在正在做的是......
setInterval(function(){
if(serverReachable()){
.... //代码行
.... //行代码
var changeIt = document.getElementById('change')
changeIt.innerHTML ='';
timeout = setInterval(function(){window.location.href =Tracker.html ;},5000);
}
} else {
clearTimeout(timeout);
timeout = null;
var changeIt = document.getElementById('change' )
changeIt.innerHTML ='offline';
}
},250);
这会使浏览器崩溃,因为我没有使用 innerHTML
打印离线,但打印整个
< div>
。我想让这个< div>
隐藏,而不是使用 innetHTML
来取消隐藏,如果条件是(在这种情况下,没有互联网连接)。
changeIt.style.visibility ='hidden';
使div消失。并且
changeIt.style.visibility ='visible';
再次显示。
I'm trying to avoid using innerHTML
because it causes my browser to crash, probably due to the 250 milliseconds refresh rate.
Anyway, I would rather have some content in an hidden <div>
and make the <div>
visible only if a certain condition is met. What's the best approach to go around this?
Basically, what I'm doing now is..
setInterval(function () {
if (serverReachable()) {
.... // lines of code
.... // lines of code
var changeIt = document.getElementById('change')
changeIt.innerHTML = '';
timeout = setInterval(function(){window.location.href = "Tracker.html";},5000);
}
} else {
clearTimeout(timeout);
timeout = null;
var changeIt = document.getElementById('change')
changeIt.innerHTML = 'offline';
}
}, 250);
This will crash my browser, because I'm not using innerHTML
to print "offline" but a whole <div>
. I want to have this <div>
hidden, and instead of using innetHTML
, to simply unhide if a condition is met (in this case, no internet connection).
Then use CSS to hide and unhide the div. You can do something like this:
changeIt.style.visibility = 'hidden';
to make the div disappear. And
changeIt.style.visibility = 'visible';
to show it again.
这篇关于JavaScript:如何隐藏/取消隐藏< div>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!