Javascript setAttribute('display','none') 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4981454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setAttribute('display','none') not working
提问by jason m
function classInfo(e){
document.getElementById('classRight').setAttribute('display','none');
alert(e);
}
I think this code is very straightforward bit it's not working, and is not hiding the element in question. I am getting my alert, which makes me think there is no issue.
我认为这段代码非常简单,它不起作用,并且没有隐藏有问题的元素。我收到警报,这让我认为没有问题。
Any help would be appreciated.
任何帮助,将不胜感激。
回答by Yi Jiang
display
is not an attribute - it's a CSS property. You need to access the style object for this:
display
不是属性 - 它是 CSS 属性。您需要为此访问样式对象:
document.getElementById('classRight').style.display = 'none';
回答by jason m
Try this:
尝试这个:
setAttribute("hidden", true);