简体   繁体   English

放入 JavaScript 脚本时 onclick 不起作用

[英]onclick doesn't work when put in JavaScript script

For the sake of simplicity:为了简单起见:

In JavaScript:在 JavaScript 中:

function f1(idName) {
    return document.getElementById(idName);
}

function f2(v1,v2) {
    f1('one').innerHTML = v1 + v2;
}

//this doesn't work
f1('one').onclick = function() {
    f2(1,2);
};

//this doesn't work, even if this one works I wouldn't use it, since I don't want to write the same function multiple times
f1('one').onclick = function f2(1,2) {
    return f1('one').innerHTML = v1 + v2;
}

//this doesn't work
f1('one').addEventListener('click', f2(v1, v2));

In HTML (Works great):在 HTML 中(效果很好):

<a ... onclick="f2(1, 2)"></a>
<p id="one"><!--output 3--></p>

I know that in JavaScript, element.onclick = should be functionName, not functionCall();我知道在 JavaScript 中,element.onclick = 应该是 functionName,而不是 functionCall(); but what's the solution for a function with parameters, do I have to use HTML onclick in this case?但是带参数的函数的解决方案是什么,在这种情况下我必须使用 HTML onclick 吗? I really want to put onclick in script though.我真的很想在脚本中添加 onclick 。

because因为

return document.getElementById('idName');  <---

You are looking for an id of idName because of the quotes makes it a string and not a reference to your variable.您正在寻找idName的 id,因为引号使其成为字符串而不是对您的变量的引用。

Your console should have error message in it saying something like Cannot read property 'onclick' of null(…)您的控制台中应该有错误消息,上面写着Cannot read property 'onclick' of null(…)

Remove the single quotes in this line.删除此行中的单引号。

function f1(idName) {return document.getElementById('idName');}

So use this instead.所以改用这个。

function f1(idName) {return document.getElementById(idName);}

The reason is that you want the variable idName to be passed to getElementsById, not the string 'idName'原因是您希望将变量 idName 传递给 getElementsById,而不是字符串 'idName'

It doesn't work because you are not attaching proper functions as your listeners它不起作用,因为您没有附加适当的功能作为您的听众

//this doesn't work, even if this one works I wouldn't use it, since I don't want to write the same function multiple times
f1('one').onclick = function f2(1,2) { return f1('one').innerHTML = v1 + v2; }

The one isn't even valid.一个甚至无效。 You can't define 1 and 2 as argument names.您不能将 1 和 2 定义为参数名称。 And the onclick must be set to a function that optionally accepts an event.并且 onclick 必须设置为一个可以选择接受事件的函数。 So instead you should:所以你应该:

f1('one').onclick = function(ev){ f2(1,2); }

For your second one对于你的第二个

// this doesn't work
f1('one').addEventListener('click', f2(v1, v2));

That one doesn't work because you are actually evaluating f2 with the variables v1 and v2, which returns undefined.那个不起作用,因为您实际上正在使用变量 v1 和 v2 评估 f2,这将返回 undefined。 So you are tring to attach undefined as a listener...所以你试图将 undefined 附加为侦听器......

Try to think of it this way: you are telling an event about a function that should be run, and the event is the one that defines what gets passed to the function as an argument.试着这样想:你正在告诉一个关于应该运行的函数的事件,而事件定义了作为参数传递给函数的内容。 In this case, most events will pass the event itself as the first argument and you need to figure out what to do from there.在这种情况下,大多数事件会将事件本身作为第一个参数传递,您需要从那里弄清楚要做什么。

If you want to get crazy and not repeat code you should look into creating a function that returns a function (so meta)如果你想变得疯狂而不是重复代码,你应该考虑创建一个返回函数的函数(如此元)

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

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