简体   繁体   English

JS变量值未显示在html中

[英]JS variable value not displaying in html

I want to display the js variable inside an html tag. 我想在html标签中显示js变量。 Here is my code: 这是我的代码:

<script>
    document.getElementById('test1').innerHTML = parseInt(localStorage.getItem("newvalue"));            
</script>

HTML Code: HTML代码:

<details>
    <summary>Section 1</summary>
    <p><b>Value: <p id="test1"></p></b></p> 
</details>

The value of id=test1 is not displaying in my web page. id = test1的值未显示在我的网页中。 Please advice. 请指教。 Thanks. 谢谢。

Make sure 确保

<script>
document.getElementById('test1').innerHTML = parseInt(localStorage.getItem("newvalue"));            
</script>

is on the button of your page, just before the </body> tag. 位于页面按钮上</body>标记之前。 Your script is probably loaded before your DOMs. 您的脚本可能在DOM之前加载。 Also/else, try to use a <span> instead of <p> . 另外,请尝试使用<span>代替<p>

It Works 有用

Either make sure the DOM call is at the bottom of your page, or call it on jQuery.ready 确保DOM调用位于页面底部,或者在jQuery.ready上调用它

$( document ).ready(function() {
    document.getElementById('test1').innerHTML = parseInt(localStorage.getItem("newvalue"));            
});

As 9Deuce said above in the comments, you can't nest p tags in p tags. 正如9Deuce上面在评论中所说,您不能将p标签嵌套在p标签中。 Try changing it to: 尝试将其更改为:

<span id="test1"></span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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