javascript - 如何从 html 标签临时将数据存储在 javascript 中?

标签 javascript html tags store

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

关于javascript - 如何从 html 标签临时将数据存储在 javascript 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25990052/

相关文章:

javascript - 有什么合适的方法可以将 d3.js 图形集成到 Facebook React 应用程序中吗?

html - 如何使 HTML 表中的颜色信息易于访问?

javascript - anchor 标记上的 HTML 属性在页面加载时执行 Javascript

ruby - 如何让 Builder 创建 <tag></tag> 而不是 <tag/>

javascript - JS 中的闭包问题

javascript - 当 Facebook 连接速度减慢时,网站加载速度缓慢

javascript - 如何在相对定位的网页中将DIV float 到内联DIV的第二行?

html - 如何增加被 href 包围的 div 按钮的可点击区域

html - 在每个标记属性上换行并在 Visual Studio HTML 代码编辑器中保持对齐

php - 从 PHP 函数执行 javascript 函数