span标记中的javascript显示值

时间:2022-11-19 19:24:54

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

在javascript中,我如何为标记分配和显示一个值,所以它出现在;

<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>

3 个解决方案

#1


3  

HTML:

HTML:

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

JS:

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>

#2


0  

Try:

尝试:

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

#3


0  

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>

#1


3  

HTML:

HTML:

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

JS:

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>

#2


0  

Try:

尝试:

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

#3


0  

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>