Html 使用 javascript 更改 onClick 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15097315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:55:09  来源:igfitidea点击:

Change onClick attribute with javascript

javascripthtmlfunction

提问by shiro

This is my function and it should change the onClick attribute of the HTML input, but if I use

这是我的函数,它应该更改 HTML 输入的 onClick 属性,但是如果我使用

document.getElementById('buttonLED'+id).onclick = "writeLED(1,1)";

it does not work at all, but if I use

它根本不起作用,但如果我使用

document.getElementById('buttonLED'+id).onclick = writeLED(1,1);

the function executes by itself! Any ideas what code do I have to use to change the onCLick attribute WITHOUT executing the function, before the button is clicked?
Here is the full function, if it matters:

函数自行执行!在单击按钮之前,我必须使用什么代码来更改 onCLick 属性而不执行函数?
这是完整的功能,如果重要的话:

function showLED(id){
    if(color == 0){
        document.getElementById('buttonLED'+id).onclick = "writeLED(1,1)";
        document.getElementById('buttonLED'+id).value="light is on";
        //document.getElementById('buttonLED'+id).disabled = false;
    }else{
        document.getElementById('buttonLED'+id).onclick = "writeLED(1,0)";
        document.getElementById('buttonLED'+id).value="light is off";
        //document.getElementById('buttonLED'+id).disabled = false;
    }
}

回答by Marcelo Teixeira Ruggeri

Well, just do this and your problem is solved :

好吧,只要这样做,你的问题就解决了:

document.getElementById('buttonLED'+id).setAttribute('onclick','writeLED(1,1)')

Have a nice day XD

祝你有个美好的一天XD

回答by DanC

You want to do this - set a function that will be executed to respond to the onclick event:

你想这样做 - 设置一个将被执行以响应 onclick 事件的函数:

document.getElementById('buttonLED'+id).onclick = function(){ writeLED(1,1); } ;

The things you are doing don't work because:

您正在做的事情不起作用,因为:

  1. The onclick event handler expects to have a function, here you are assigning a string

    document.getElementById('buttonLED'+id).onclick = "writeLED(1,1)";
    
  2. In this, you are assigning as the onclick event handler the result of executing the writeLED(1,1) function:

    document.getElementById('buttonLED'+id).onclick = writeLED(1,1);
    
  1. onclick 事件处理程序期望有一个函数,在这里您分配一个字符串

    document.getElementById('buttonLED'+id).onclick = "writeLED(1,1)";
    
  2. 在此,您将执行 writeLED(1,1) 函数的结果分配为 ​​onclick 事件处理程序:

    document.getElementById('buttonLED'+id).onclick = writeLED(1,1);
    

回答by pmitchell

The line onclick = writeLED(1,1)means that you want to immediatelyexecute the function writeLED(arg1, arg2) with arguments 1, 1 and assign the return value; you need to instead create a function that will execute with those arguments and assign that. The topmost answer gave one example - another is to use the bind() function like so:

该行 onclick = writeLED(1,1)表示您要立即执行带有参数 1、1 的函数 writeLED(arg1, arg2) 并分配返回值;您需要创建一个函数,该函数将使用这些参数执行并分配它。最上面的答案给出了一个例子——另一个是像这样使用 bind() 函数:

    var writeLEDWithSpecifiedArguments = writeLED.bind(this, 1,1);
    document.getElementById('buttonLED'+id).onclick = writeLEDWithSpecifiedArguments;

回答by Muhammad Yasir

Using Jquery instead of Javascript, use 'attr'property instead of 'setAttribute'

使用 Jquery 代替 Javascript,使用'attr'属性代替'setAttribute'

like

喜欢

$('buttonLED'+id).attr('onclick','writeLED(1,1)')

回答by Sam

Another solution is to set the 'onclick' attribute to a function that returns your writeLED function.

另一种解决方案是将“onclick”属性设置为返回 writeLED 函数的函数。

document.getElementById('buttonLED'+id).onclick = function(){ return writeLED(1,1)};

This can also be useful for other cases when you create an element in JavaScript while it has not yet been drawn in the browser.

当您在 JavaScript 中创建尚未在浏览器中绘制的元素时,这对于其他情况也很有用。

回答by Arindam

You are not actually changing the function.

你实际上并没有改变功能。

onClickis assigned to a function (Which is a reference to something, a function pointer in this case). The values passed to it don't matter and cannotbe utilised in any manner.

onClick被分配给一个函数(它是对某物的引用,在这种情况下是一个函数指针)。传递给它的值无关紧要,不能以任何方式使用。

Another problem is your variable colorseems out of nowhere.

另一个问题是您的变量color似乎无处不在。

Ideally, inside the function you should put this logic and let it figure out what to write. (on/off etc etc)

理想情况下,您应该在函数内部放置此逻辑并让它弄清楚要写什么。(开/关等)