简体   繁体   中英

Using javascript to replace the html content

I am trying to replace the content of id='js' with javascript, but this is not working for me. here is the code.

<html>
    <head>
       <title></title>

       <script type="text/javascript">
            var ball = 0;
            function count_balls(ball){
                var count = ball + 1;
                return count;
                }
            document.getElementById('js').innerHTML = count_balls(0);
       </script>
    </head>
    <body>
        <p id='js'>Result Here</p>
    </body>
</html>

Because the element is not initialized yet, it's not working. You can either move the javascript to the end of the HTML (preferred) as shown below:

<html>
<head>
   <title></title>


</head>
<body>
    <p id='js'>Result Here</p>

       <script type="text/javascript">
        var ball = 0;
        function count_balls(ball){
            var count = ball + 1;
            return count;
            }

        document.getElementById('js').innerHTML = count_balls(0);
   </script>
</body>

Or you can use jquery document.ready to achieve the same thing.

i have changed inner html content of my website using document.getElemntById("xyz").innerHTML = "baby" ...but after refreshing page the changes gone. can anyone pls help me to fix this problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM