简体   繁体   English

如何使用 javascript 在 FF 中切换 div 的可见性? (IE 和 Chrome 工作正常)

[英]How do you toggle the visibility of a div in FF with javascript? (IE and Chrome work fine)

So, I'm trying to toggle the visibility of a div layer using javascript and this is what it looks like:所以,我正在尝试使用 javascript 切换 div 层的可见性,这就是它的样子:

document.all["layer1"].style.visibility='visible';

and

document.all["layer1"].style.visibility='hidden';

What would the syntax look like for this if I'm trying to do it in Firefox?如果我尝试在 Firefox 中执行此操作,语法会是什么样子? If I know that, then I'll add in an if statement to check if the browser is firefox and use the alternate code.如果我知道,那么我将添加一个 if 语句来检查浏览器是否为 firefox 并使用备用代码。

document.all is a not a supported function, nor is it in the spec for the DOM ( Here is more on that). document.all不是受支持的函数,也不是DOM的规范( 以下是更多内容)。 You will have to grab the div by another method. 你将不得不通过另一种方法获取div。

querySelector querySelector

document.querySelector('div#layer1').style.visibility = 'hidden';    
document.querySelector('div#layer1').style.visibility = 'visible';

OR 要么

getElemenyById getElemenyById

document.getElementById('layer1').style.visibility = 'hidden';
document.getElementById('layer1').style.visibility = 'visible';

are two major methods. 是两种主要方法。

Don't use document.all[] . 不要使用document.all[] It is not supported by all browsers, and is largely an artifact of the IE4 days. 并非所有浏览器都支持它,并且在很大程度上是IE4时代的工件。 Instead use document.getElementById() to access the <div> 's id attribute: 而是使用document.getElementById()来访问<div>的id属性:

document.getElementById("layer1").style.visibility = 'hidden';
document.getElementById("layer1").style.visibility = 'visible';

//class named layer1 //类名为layer1

document.querySelectorAll('.layer1').style.display='none';
document.querySelectorAll('.layer1').style.display='block';


//id named layer1
document.querySelector('#layer1').style.display='none';
document.querySelector('#layer1').style.display='block';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM