简体   繁体   English

尝试在不编辑html代码的情况下更改按钮的文本

[英]trying to change the text of the button without editing the html code

I'm trying to change the text of the button without editing the HTML code but it doesn't really work 我试图在不编辑HTML代码的情况下更改按钮的文本,但实际上并没有用

Here is the code: 这是代码:

document.querySelector(".game-button").onClick = function knop(knop) {
    document.querySelector(".game-button").innerHTML = "Reset spel";
}

HTML: HTML:

<button class="game-button">Start spel</button>

If I may suggest: 如果我可以建议:

var gameButton = document.querySelector(".game-button")
gameButton.onclick = function() {
    gameButton.innerHTML = "Reset spel";
}

I've put the gamebutton in a variable so it is only referenced once. 我已经将gamebutton放在一个变量中,因此它只被引用了一次。 Since this is a function connected to an onclick event, naming the function is not needed. 由于这是一个与onclick事件相关的函数,因此无需命名该函数。 Also, you are not passing or using any arguments, so there is no need to put anything between the (). 另外,您没有传递或使用任何参数,因此无需在()之间放置任何内容。

try this code 试试这个代码

document.querySelector(".game-button").onClick = function knop() {
    this.innerHTML = "Reset spel";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM