简体   繁体   English

使用javascript更改按钮文字/值

[英]Change the button text/value on click with javascript

My goal is to include Element.ID within function and then, fetch their value or text. 我的目标是将Element.ID包含在函数中,然后获取它们的值或文本。 It is important to reduce code lines as well because there are many others buttons with the same rule. 减少代码行也很重要,因为还有许多其他按钮具有相同的规则。

So, I tried the below code and many others to get the appropriate results. 因此,我尝试了以下代码以及许多其他代码,以获得适当的结果。

How do I fix it? 我如何解决它?

 var el = document.getElementById("p1"); var id = document.getElementById("p1").id; el.addEventListener("click", modifyText(id), false); function modifyText(e) { var x = e.value; if (x < 40) { e.value = 1; } }; 
 <input id="p1" type="button" class="button" value=0> <input id="pn" type="button" class="button" value=0> 

Well, the second argument to .addEventListener() has to be a function reference, not "loose" code to execute. 好吧, .addEventListener()的第二个参数必须是函数引用,而不是要执行的“松散”代码。 So, if you want to call another function and pass it an argument, the line should be: 因此,如果要调用另一个函数并将其传递给参数,则该行应为:

el.addEventListener("click", function(){modifyText(id)}, false);

Now, you are making quite a bit out of the element's id , but you really only need the id to get your initial reference to the element. 现在,您已经从元素的id ,但是您实际上只需要id即可获得对该元素的初始引用。 Once you've got that, you can just work with it. 一旦知道了,就可以使用它。

You've got a lot of unnecessary code here. 您这里有很多不必要的代码。 Also, I'm assuming (perhaps incorrectly) that you want both buttons to have the same click behavior, so that's what I'm proceeding with. 另外,我假设(也许是错误地)您希望两个按钮都具有相同的click行为,所以这就是我要进行的工作。

 // You only need to get a reference to the element in question var el1 = document.getElementById("p1"); var el2 = document.getElementById("pn"); // Set up each button to use the same click callback function // The second argument needs to be a function reference el1.addEventListener("click", modifyText); el2.addEventListener("click", modifyText); function modifyText(){ // When inside of an event handler, "this" refers to the element // that triggered the event. if (this.value < 40 ) { this.value = 1; } } 
 <input id = "p1" type="button" class="button" value=0> <input id = "pn" type="button" class="button" value=0> 

Event listener callbacks tend to be executed with the execution context of the element (unless otherwise modified, or using Arrow functions). 事件侦听器回调通常与元素的执行上下文一起执行(除非进行其他修改或使用Arrow函数)。 This means you can just use this keyword to refer to the element. 这意味着您可以仅使用this关键字来引用元素。 So inside the callback you could use this.value / this.innerText (depending on type of element) 因此,在回调中,您可以使用this.value / this.innerText (取决于元素的类型)

function modifyText() {
 var x = this.value;
 if ( x < 40 ) {
  this.value = 1;
 }
}

Also the way you called addEventListener was wrong. 同样,您调用addEventListener的方式是错误的。

.addEventListener("click", modifyText(id), false); .addEventListener(“ click”,ModifyText(id),false);

This will execute modifyText immediately and use the return value of the function as the callback. 这将立即执行modifyText并将函数的返回值用作回调。 And since your function doesnt return anything nothing is set as the callback. 并且由于您的函数不返回任何内容,因此未将任何内容设置为回调。

If you wanted to pass a variable to an event callback you would do it like the following 如果您想将变量传递给事件回调,则可以像下面这样进行操作

el.addEventListener("click", modifyText.bind(el,yourValue),false);

You would then need to modify the function definition 然后,您需要修改函数定义

function modifyText(passedValue,event) {

}

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

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