存储在变量中的innerHTML的修改不起作用 [英] Modification of innerHTML stored in variable not working
本文介绍了存储在变量中的innerHTML的修改不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将页面的显示区域存储在变量(代码中的 display
)中,即显示的innerHTML 在我的例子中code> div,但是当我这样做时,我根本无法修改其内容。
I want to store the display area of a page in a variable (display
in the code), i.e. the innerHTML of the display
div in my example, but when I do that I cannot modify its content at all.
function stored() {
var display = document.getElementById("display").innerHTML;
display = "Bonjour";
}
function notStored() {
document.getElementById("display").innerHTML = "Hello";
}
<button onclick="stored()">Stored in variable</button>
<button onclick="notStored()">Not stored in variable</button>
<div id="display"></div>
我该怎么办?
感谢您的帮助。
推荐答案
那是因为您为 display
变量分配了一个新值。你想要做的是将 innerHTML
的显示
设置为一个变量,例如存储
。通过这样做,您将所需的 innerHTML
存储为变量。
That's because you assigned a new value to your display
variable. What you want to do is to set the innerHTML
of display
to a variable, eg stored
. By doing that, you stored your desired innerHTML
as a variable.
function stored() {
var stored = "Bonjour";
var display = document.getElementById("display").innerHTML = stored;
}
function notStored() {
document.getElementById("display").innerHTML = "Hello";
}
<button onclick="stored()">Stored in variable</button>
<button onclick="notStored()">Not stored in variable</button>
<div id="display"></div>
这篇关于存储在变量中的innerHTML的修改不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文