简体   繁体   English

javascript onclick调用函数

[英]javascript onclick call a function

I found that code on internet and it works fine to create a button 我在互联网上找到了该代码,可以很好地创建一个按钮

  document.write(nomedispositivo)
  var r=$('<input/>').attr({//início botão
  type: "button",
  id: "field" ,
  value: "Liga",

But if I insert the line: onclick:switchLED() where switchLED is a function the button not appear where is the problem? 但是,如果我插入以下行:onclick:switchLED()其中switchLED是一个函数,则按钮没有出现在哪里呢?

  document.write(nomedispositivo)
  var r=$('<input/>').attr({//início botão
  type: "button",
  id: "field" ,
  value: "Liga",
  onclick:switchLED()

Why add the click handler this way in the first place? 为什么首先要以这种方式添加点击处理程序? You're using jQuery, so use jQuery. 您正在使用jQuery,因此请使用jQuery。 Just add the handler to the jQuery element you already have: 只需将处理程序添加到您已有的jQuery元素中:

var r=$('<input/>').attr({
  type: "button",
  id: "field" ,
  value: "Liga"
});

r.click(switchLED);

Since r is a jQuery element, you can use the click(function) function to add a function reference to that element's click event handler. 由于r是jQuery元素,因此可以使用click(function)函数向该元素的click事件处理程序添加函数引用。

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

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