简体   繁体   中英

javascript display value in span tag

in javascript, how can i assign and display a value to tag , so it appears in;

<span id=tag></span>

This needs to be set on the pageload.

i thought it was something like:

<script>
document.getElementById('tag').innerHTML = 0;
</script>

HTML:

<span id="tag"></span>

JS:

<script type="text/javascript">
   window.onload = function(){
     var text = document.createTextNode("0");
     document.getElementById('tag').appendChild(text);
   }
</script>

Although it would be much easier to just do:

<span id="tag">0</span>

You can try this also

<html>
<head>
<script>
function myFunction() {
    var number = "0";
    document.getElementById("tag").innerHTML = number;
}
</script>
</head>
<body onload="myFunction()">

<h1>"the value is: " <span id="tag"></span></h1>

</body>
</html>

Try:

<script>
  window.onload = function(){
        document.getElementById('tag').innerHTML = 0;
  };
</script>

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