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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:04:23  来源:igfitidea点击:

setAttribute('display','none') not working

javascriptdom

提问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

displayis 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);