如何将值解析为id onclick函数[重复]

I am trying to to parse a value to id="demo" onclick function

onclick函数的位置

<p  onclick="myFunction()">Value.</p>

值解析到的位置

<span id="demo"> </span>

* Onclick功能脚本

<script>
function myFunction() {
 var k = 30;
  document.getElementById("demo").value = $k;
}
</script>

I am not able to parse the value to id="demo" i don't where i am going wrong.

评论
nodio
nodio

You need use innerText instead of .value for span tag, .value use for input tag

function myFunction() {
 var k = 30;
  document.getElementById("demo").innerText = k;
}
function myFunction() {
 var k = 30;
  document.getElementById("demo").innerText = k;
}
<p  onclick="myFunction()">Value.</p>


<span id="demo"> </span>
点赞
评论