简体   繁体   中英

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?

  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. Just add the handler to the jQuery element you already have:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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