更改按钮文本的onclick [英] Changing button text onclick
问题描述
当我点击这个按钮,我想要的值来改变。 HTML:
When I click on this button, I want the value to change. HTML:
<input onclick="change()" type="button" value="Open Curtain" id=myButton1"></input>
使用Javascript:
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?
推荐答案
如果我正确地理解你的问题,你要打开窗帘和关闭窗帘之间进行切换 - 切换到开帷幕如果它的关闭,反之亦然。如果这是你所需要的,这将正常工作。
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.
更新
我错了。还不如我刚才所说,这个
将不参考元素本身。您可以使用此:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!