function hide_article() {
var htmlElement = document.getElementsByTagName("article")[0];
if (htmlElement.getAttribute('visibility') == 'visible'){
htmlElement.style.visibility = 'hidden';
console.log("hiding");
}
else {
htmlElement.style.visibility = 'visible';
console.log("showing");
}
};
此代码应切换文章的可见性。然而,它总是在不应该运行的时候运行“else”代码块。为什么是这样?干杯
最佳答案
html 元素没有 'visibility'
属性,但您已经使用了 style.visibility
属性,因此:
if (htmlElement.style.visibility != 'hidden'){
...
会做你想做的事。
关于javascript - 检查 html 元素的属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24834840/