使用javascript更改onClick属性 [英] Change onClick attribute with javascript

查看:89
本文介绍了使用javascript更改onClick属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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);

函数自行执行!
任何想法在点击按钮之前用什么代码来改变onCLick属性而不执行函数?

这里是完整的函数,如果它很重要:

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:

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


推荐答案

,只要做到这一点,你的问题就解决了:

Well, just do this and your problem is solved :

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

祝您有个愉快的一天XD

Have a nice day XD

这篇关于使用javascript更改onClick属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆