如何动态分配 JavaScript 的“onclick”属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3007336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:52:32  来源:igfitidea点击:

How do you assign a JavaScript 'onclick' attribute dynamically?

javascript

提问by Hyman Roscoe

I'm creating a button dynamically using JavaScript and at the same time assigning attributes such as 'ID', 'type' etc and also 'onclick' in order to trigger a function.

我正在使用 JavaScript 动态创建一个按钮,同时分配诸如“ID”、“类型”等以及“onclick”等属性以触发功能。

All works fine apart from the assignment of the 'onclick'. When clicked, the button is not triggering the function as it is supposed to. the function I'm trying to run is 'navigate(-1)' as seen below.

除了“onclick”的分配之外,一切正常。单击时,该按钮不会像预期的那样触发该功能。我试图运行的函数是“导航(-1)”,如下所示。

Where am I going wrong?

我哪里错了?

Here's my code:

这是我的代码:

function loadNavigation() {
  var backButton;
  backButton = document.createElement('input');
  backButton.ID = 'backButton';
  backButton.type = 'button';
  backButton.value='Back';
  backButton.onclick = 'navigate(-1)';
  document.body.appendChild(backButton);
}

回答by nico

As the other said you should assign a function.

正如另一个所说,你应该分配一个功能。

Just wanted to point out that in this case you want to pass a value so you need to assign an anonymous function (or a named function defined inline) like

只是想指出,在这种情况下,您想要传递一个值,因此您需要分配一个匿名函数(或内联定义的命名函数),例如

button.onclick = function() {otherfunction(parameter)};

If the function you want to assign does NOT require a parameter you can use it directly

如果您要分配的功能不需要参数,您可以直接使用它

button.onclick = otherfunction;

Note that there is no parenthesis in this case

请注意,在这种情况下没有括号

button.onclick = otherfunction(); // this doesn't work

won't work as it will call otherfunctionas soon as it is parsed

不会工作,因为它会otherfunction在解析后立即调用

回答by jasonmw

you are assigning text to the onclick, try assigning a function.

您正在为 onclick 分配文本,请尝试分配一个功能。

backButton.onclick = function(){navigate(-1);};

回答by Quentin

You have to assign a function, not a string.

您必须分配一个函数,而不是一个字符串。

backButton.onclick = function wastefulDuplicationOfBackButton () {
    navigate(-1);
}

回答by CMS

You should assign a function, not a string:

您应该分配一个函数,而不是一个字符串:

//...
backButton.onclick = function () {
  navigate(-1);
};
//...

回答by Eli Grey

Use a function instead of a string. For example,

使用函数而不是字符串。例如,

backButton.onclick = function () { navigate(-1); };

回答by Kevin Le - Khnle

backButton.onclick = function() { navigate(-1); }

回答by mplungjan

In case this question is passed as a dupe, here is how to do it in current browsers

如果这个问题是作为一个骗子传递的,这里是如何在当前浏览器中做到这一点

ES6

ES6

backButton.addEventListener("click",() => history.back());

Older but newer than onclick

比 onclick 旧但新

backButton.addEventListener("click",function() { history.back() });