为什么这个setAttribute函数不起作用?

时间:2014-05-24 19:23:36

标签: javascript html dom getelementbyid setattribute

我希望我的名字与#34; fn_warn"单击提交时可见。

我的html和js代码是,

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
    First name
    <input id="first_name" type="text"/>
    <p id="fn_warn" style=" visibility: hidden; color: red;">#Please enter a valid name...</p>
    <input class="button" onclick="tada();" type="submit" value="Submit" /> <input class="button" type="reset" />
</body>
</html>

<script>
    function tada(){
       var x= document.getElementById("fn_warn");
       x.setAttribute("visibility","visible");
    }  
</script>

1 个答案:

答案 0 :(得分:1)

CSS规则不是属性。请使用style属性,因为规则是内联的:

function tada(){
    var x = document.getElementById("fn_warn");
    x.style.visibility = "visible";
}