[英]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.