如何从html标签将数据临时存储在java脚本中

How to store data temporarily in java script from a html tag?

本文关键字:存储 java 脚本 数据 html 标签      更新时间:2023-09-26

我是JavaScript的新手,这里有一个我不知道如何克服的挑战!

这是我尝试过的示例代码:

<body>
    <p id="hello">Hello world.</p> 
    
    
    <script type="text/javascript">
    
    var store,newString,finalString;
    //i want to store the content of <p> tag in store variable
    store=?
    newString="I am Shuvrow";
    finalString = store + newString;
    //finally i want to replace the content of <p> tag by finalString variable
      
    </script>
</body>
感谢

使用文档元素的innerHTML属性,例如:

<body>
    <p id="hello">Hello world.</p> 
    
    
    <script type="text/javascript">
    
        var store,newString,finalString;
        // Get the contents of the element with the ID of 'hello' and assign it to the store variable.
        store = document.getElementById('hello').innerHTML;
        newString="I am Shuvrow";
        finalString = store + newString;
        // Replace the contents of the element with the ID of 'hello' with the value of the finalString variable.
        document.getElementById('hello').innerHTML = finalString;
    </script>
</body>

相关文章: