试图显示和隐藏部分HTML文档

时间:2013-01-09 11:43:57

标签: javascript html dom

以下是我的网页的HTML:

     <!DOCTYPE html>

        <html>

    <body>

    <link href = "sites.css" type = "text/css" rel = "stylesheet"/>

    <image src = "beach_houses.jpg" id = "sites">

    <form id = "details">
     Please enter your name: <input type = "text" id = "name"><br/><br/>
     Please provide a valid e-mail: <input type = "text" id = "e-mail"><br/><br/>
     I am on a tour<input type = "checkbox" id = "status">

    <div id = "further"><br/>
     Indicate your gender 
     M<input type = "radio" name = "gender"value = "male" id = "M"> 
     F<input type = "radio" name = "gender" value = "female" id = "F">
  </div><br/></br>
    <input type = "submit" value = "submit!">
</form>

    <br/><br/><div id = "error" ></div>

    </body>

    <script src = "function.js" type = "text/javascript"></script>

    </html>

这是javascript:

Images.onclick = function() {


clearInterval(iHandle);
}


function prepare() {

document.getElementById("status").onclick = function() {

    if (document.getElementById("status").checked) 

        document.getElementById("further").style.display = "block";
    else 

        document.getElementById("further").style.display = "none";

    document.getElementById("further").style.display = "none";

};


document.getElementById("details").onsubmit = function() {

    if(document.getElementById("name").value == "") {

        document.getElementById("error").innerHTML = "Please provide a name!";

        document.getElementById("error").style.color = "red";

        return false;

    }

    else if (document.getElementById("e-mail").value == "") {

        document.getElementById("error").innerHTML = "Please provide a valid e-mail address!";

        document.getElementById("error").style.color = "red";

        return false;

    }
};



}



window.onload = function() {

    prepare();
};

我们的想法是,当我选中了我的旅行复选框时,会弹出表明您的性别问题。然而,恰恰相反;这个问题在默认情况下似乎是可见的,当我点击复选框时会变得不可见。为什么是这样?

提前感谢你 Saad的

1 个答案:

答案 0 :(得分:1)

更改<div id = "further"&gt;到<div id = "further" style="display:none">

document.getElementById("status").onclick = function() {

    if (document.getElementById("status").checked) {

        document.getElementById("further").style.display = "block";
    }
    else {
        document.getElementById("further").style.display = "none";
    }
};