开发者

Display a div using a javascript function

I'd like to display a div on a webpage when a user clicks on a button.

Does someone know how to do this ?

My code, so far, is :

        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso 8859-1" />
        </head>
        <body>
          <input i开发者_如何学God="text" type="text" size="60" value="Type your text here" />
          <input type="button" value="When typing whatever text display the div balise on the page" onclick="check();" />

          <script type="text/javascript">

        function check() {
              //Display my div balise named level0;
        }

          </script>

        </body>
      </html>

Thanks,

Bruno

EDIT: All my code (I've erased it because it was too long and not very clear)


You can use document.createElement("div") to actually make the div. Then you can populate the div using innerHTML for the text. After that, add it to the body using appendChild. All told, it can look like this:

function check() {
    var div = document.createElement("div");
    div.innerHTML = document.getElementById("text").value;
    document.body.appendChild(div);
}

This will add a div every time the button is pressed. If you want to update the div each time instead, you can declare the div variable outside the function:

var div;
function check() {
    if (!div) {
        div = document.createElement("div");
        document.body.appendChild(div);
    }

    div.innerHTML = document.getElementById("text").value;
}

If you have the div already in the page with an id of "level0", try:

function check() {
    var div = document.getElementById("level0");
    div.innerHTML = document.getElementById("text").value;
}


A quick search on google gave me this example:

Demo of hide/show div

The source-code for that example is:

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
<html> 
<head> 
<title>Demo of Show hide div layer onclick of buttons</title> 
<META NAME="DESCRIPTION" CONTENT="Displaying and hiding div layers through button clicks"> 
<META NAME="KEYWORDS" CONTENT="Show layer, hide layer, display div, hide div, button on click, button on click event, div property, div style set"> 
<style type="text/css"> 
div {
position: absolute;
left: 250px;
top: 200px;
background-color: #f1f1f1;
width: 280px;
padding: 10px;
color: black;
border: #0000cc 2px dashed;
display: none;
}
</style> 


<script language="JavaScript"> 
function setVisibility(id, visibility) {
document.getElementById(id).style.display = visibility;
}

</script> 
</head> 
<body> 

<input type=button name=type value='Show Layer' onclick="setVisibility('sub3', 'inline');";><input type=button name=type value='Hide Layer' onclick="setVisibility('sub3', 'none');";> 

<div id="sub3">Message Box</div> 
<br><br> 
</body> 
</html>


Paste this code somewhere in your body

      <div id="myDiv" style="display:none">
          Hello, I am a div
      </div>

Add this snippet into your check() function to display the otherwise-hidden content.

      document.getElementById("myDiv").style.display = "block";

You could also change the div content programmatically thus:

      document.getElementById("myDiv").innerHTML = "Breakfast time";

... would change the text to 'Breakfast time'.


You might want to look into jquery, it'll make your life 100 times easier. Jquery is a javascript library (script) that you include and it allows you to manipulate the DOM very easily.

Start by adding the latest Jquery to your head which will allow you to use something like $(document).ready( ) The function inside .ready( fn ) is a callback function; it get called when the document is ready.

$("#lnkClick") is a selector (http://api.jquery.com/category/selectors/)

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> 

        <script type="text/javascript">
            $(document).ready( function() {
                $("#lnkClick").click( function() {
                    $("#level0").attr("style", "display: block;width: 100px; height: 100px; border: solid 1px blue;");
                });
             });
         </script>
    </head>
    <body>
         <div id="level0" style="display:none;">
         </div>

         <a href="#" id="lnkClick">Click me</a>
    </body>
</html>

Of course this code can be made cleaner. You want to check: http://api.jquery.com/click/ There are plenty of examples.

Best of luck with Jquery!


you really should be using jquery , there's a little bit of a learning curve but once you get it, developing web apps is much easier.

<html>
<head>
<script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
<script>
 $(document).ready(function() {
   $("#show_div_button").click(function() {
      $("#div_to_show").show();
      return false;
   });
});
</script>
</head>
<body>
  <a href="#" id="show_div_button">Click Me to Show the Div</a>
  <div style="display:none" id="div_to_show">I will be shown when the link is clicked</div>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜