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