简体   繁体   English

如何仅使用JavaScript定义按钮元素的onclick属性?

[英]How do I define a button element's onclick attribute using just JavaScript?

As it says in the title, I'm trying to create a button element in JavaScript, but when I try to set its onclick attribute, it just ignores it. 就像标题中所说的那样,我试图在JavaScript中创建一个button元素,但是当我尝试设置其onclick属性时,它只会忽略它。 I've looked at other questions with this topic on this site (such as this one ), but everyone answers to put it in with HTML, like so: 我在本网站(例如this )上也查看了与此主题相关的其他问题,但是每个人都回答将其放入HTML中,例如:

<button id="buttonid" onclick="click(this);">

But my program dynamically creates the HTML buttons entirely with JavaScript (they're aligned on a table, so using a for loop is very useful. Plus I'm more familiar with JavaScript). 但是我的程序完全使用JavaScript动态创建HTML按钮(它们在表格上对齐,因此使用for循环非常有用。此外,我对JavaScript更加熟悉)。 Like this: 像这样:

var b = document.createElement("button");
b.id = "buttonid";
b.onClick = "click(this);"; //I've tried many variations with no success
document.appendChild(b);

The button element is never declared in the HTML. 在HTML中永远不会声明button元素。 I need a way to add the onclick attribute using JavaScript alone. 我需要一种单独使用JavaScript添加onclick属性的方法。 I'm not very experienced with JQuery, so I would prefer just JavaScript. 我对JQuery不太了解,所以我更喜欢JavaScript。

Thank you in advance. 先感谢您。

PS, I've tried all the following methods with both lowercase .onclick and camelcase .onClick : PS,我已经尝试过使用小写.onclick和camelcase .onClick的以下所有方法:

b.onClick = "click(this);"; //just simply doesn't work
b.onClick = click(this);    //actually runs the program there, with some ambiguous "this" object
b.onClick = click;          //passes the actual function, but disallows the ability to pass the button's object (via "this")

Try this 尝试这个

  var b = document.createElement("button"); b.onClick = function(){ alert("on click handler"); }; 

You can use addEventListener to add the listener to the button and bind to pass the button as this for the handler function. 您可以使用的addEventListener到监听器添加到按钮并绑定到通过按钮this对于处理函数。

b.addEventListener(`click`, click.bind(b));

Or if you want to use onClick , just use bind : 或者,如果您想使用onClick ,只需使用bind

b.onClick = click.bind(b);

Try b.onClick = function(){ console.log("test"); 试试b.onClick = function(){console.log(“ test”); } }

I am not sure what your "click" function is. 我不确定您的“点击”功能是什么。 Maybe it isn't declared yet or something. 也许它尚未被声明。 Or what is happening is your function is named click "function click(){...}" which instead needs to be click = function(){...} 或正在发生的事情是您的函数名为click“ function click(){...}”,而需要单击= function(){...}


 (function(){ // create the button var buttonB = document.createElement('button'); buttonB.id = 'buttonB'; var textB = document.createTextNode('Button B'); buttonB.appendChild( textB ); // set the click event on the button buttonB.addEventListener('click', function(){ // console.log('test'); // console.log( this ); clickFunc( this ); }); // populate the DOM-body with the button document.body.appendChild( buttonB ); })(); function clickFunc( that ){ console.log( that ); } 

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

相关问题 如何更改按钮的onclick属性所引用的功能? - How do I change the function a button's onclick attribute refers to? 如何使用 Javascript 将样式绑定到 onclick 元素? - How do I bind style to onclick element using Javascript? 如何使用Javascript从按钮获取onclick的值 - How do I get the value of onclick from a button using Javascript 如何通过 Selenium 的 onclick 属性来定位 javascript 元素? - How to target javascript element by it's onclick attribute via Selenium? 如何使用javascript获取元素的自定义属性值 - How do I get the custom attribute value of an element using javascript 如何使用javascript更改元素的属性值 - How do I change the attribute values of an element using javascript 如何使用javascript onclick()获取元素的属性值? - How to get the attribute value of an element using javascript onclick()? 如何使用JavaScript从父元素的父元素ID中获取元素的属性? - How can I get an element's attribute from the parent's parent element's ID using JavaScript? 我如何 select 仅用于 Javascript onclick 回调的元素的背景空格? - How do I select only the <body> element's background whitespace for a Javascript onclick callback? 如何使用javascript从onclick按钮获取光标放置元素 - How to get cursor placed element from onclick button using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM