检查html元素的属性不起作用

时间:2014-07-18 22:43:56

标签: javascript

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”代码块。为什么是这样?干杯

1 个答案:

答案 0 :(得分:2)

html元素没有'visibility'属性,但您已经使用了style.visibility,所以:

if (htmlElement.style.visibility != 'hidden'){
...

会做你想做的事。