使用 Javascript 添加 onClick 函数不成功

Dav*_*bon 4 html javascript onclick

所以我尝试向使用 Javascript 创建的按钮添加 onClick 属性。显然,我使用 Javascript 来添加它,但是它不会通过,因为当我检查网页上的元素时,它没有显示 onClick 属性。这是我必须创建按钮的代码

var buttonExponent = document.createElement('button');
buttonExponent.innerHTML = '^';
buttonExponent.onclick = function(){appendExpr(this.className);};   <---This is my problem
buttonExponent.className = 'button operator ' + expression.id + ' top';
buttonExponent.style.marginTop = '0px';
buttonExponent.style.marginLeft = '-6px';
container.appendChild(buttonExponent);
Run Code Online (Sandbox Code Playgroud)

其余的功能都可以正常工作,但是 onClick 属性根本不会添加。我尝试过多种方法,但都不起作用

编辑:我想我应该添加网页上的输出,该元素具有此 HTML

<button class="button operator expr1 top" style="margin-top: 0px; margin-left: -6px;">^</button>
Run Code Online (Sandbox Code Playgroud)

当它应该看起来像这样的时候

<button onClick="appendExpr(this.className);" class="button operator expr1 top" style="margin-top: 0px; margin-left: -6px;">^</button>
Run Code Online (Sandbox Code Playgroud)

Sci*_*ter 7

修改element.onclickDOM 属性不会修改元素的onclick属性。它们是两个独立的数据,令人困惑。

要设置属性,请使用element.setAttribute('onclick','code here').