Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When I click on myButton1 button, I want the value to change to Close Curtain from Open Curtain .

(当我点击myButton1按钮时,我希望该值从Open Curtain更改为Close Curtain Open Curtain 。)


HTML:

(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?

(按钮正在显示打开的窗帘,我希望它改为关闭窗帘,这是正确的吗?)

  ask by Anthony Do translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
352 views
Welcome To Ask or Share your Answers For Others

1 Answer

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";
}

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.

(请注意,您不需要在document.getElementById("myButton1")上下文中调用document.getElementById("myButton1") ,因为它在myButton1上下文中被调用 - 我将在稍后阅读有关JS的书籍时通过上下文了解我的意思。)

UPDATE :

(更新 :)

I was wrong.

(我错了。)

Not as I said earlier, this won't refer to the element itself.

(不像我之前所说, this不会涉及元素本身。)

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";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...