问题描述
当我单击 myButton1
按钮时,我希望将值从 Open Curtain
更改为 Close Curtain
.
HTML:
When I click on myButton1
button, I want the value to change to Close Curtain
from Open Curtain
.
HTML:
<input onclick="change()" type="button" value="Open Curtain" id="myButton1"></input>
Javascript:
function change();
{
document.getElementById("myButton1").value="Close Curtain";
}
按钮现在显示打开的窗帘,我希望它更改为关闭窗帘,这是正确的吗?
The button is displaying open curtain right now and I want it to change to close curtain, is this correct?
推荐答案
如果我正确理解了您的问题,您想在Open Curtain"和Close Curtain"之间切换——如果它已关闭,反之亦然.如果这就是您所需要的,这将起作用.
If I've understood your question correctly, you want to toggle between 'Open Curtain' and 'Close Curtain' -- changing to the 'open curtain' if it's closed or vice versa. If that's what you need this will work.
function change() // no ';' here
{
if (this.value=="Close Curtain") this.value = "Open Curtain";
else this.value = "Close Curtain";
}
请注意,您不需要在更改中使用 document.getElementById("myButton1")
,因为它是在 myButton1 的 上下文 中调用的代码>——我所说的上下文是什么意思,你将在阅读有关 JS 的书籍后了解.
Note that you don't need to use document.getElementById("myButton1")
inside change as it is called in the context of myButton1
-- what I mean by context you'll come to know later, on reading books about JS.
更新:
我错了.不像我之前说的那样,this
不会引用元素本身.你可以使用这个:
I was wrong. Not as I said earlier, this
won't refer to the element itself. You can use this:
function change() // no ';' here
{
var elem = document.getElementById("myButton1");
if (elem.value=="Close Curtain") elem.value = "Open Curtain";
else elem.value = "Close Curtain";
}
这篇关于更改按钮文本 onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!