javascript 在 onclick 函数中传递多个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29478462/
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-10-28 10:36:03  来源:igfitidea点击:

pass multiple parameter in an onclick function

javascriptjquery

提问by Neero

I want to pass three parameters to this onclick()function. but it gives me a syntax error.

我想向这个onclick()函数传递三个参数。但它给了我一个语法错误。

element += '<div class="cancel" onclick="remove_game(' + game.id + ',\'' + cellstyle + ',\'' + cellid +'\');">';

It calls the function correctly, but it gives the error below

它正确调用了该函数,但给出了以下错误

SyntaxError: missing ) after argument list


remove_game(6,'2, 'game6-cell2);

How can i resolve this issue?

我该如何解决这个问题?

回答by Scott Hunter

Your ''s and commas are off, so that the second argument appears to be '2, ', and then there isn't a comma before the third. Assuming you want quotes around the 3rd argument, you'd need something like this:

您的''s 和逗号已关闭,因此第二个参数似乎是'2, ',然后在第三个参数之前没有逗号。假设你想在第三个参数周围加上引号,你需要这样的东西:

element += '<div class="cancel" onclick="remove_game(' + game.id + ',' + cellstyle + ',\'' + cellid +'\');">';

element += '<div class="cancel" onclick="remove_game(' + game.id + ',' + cellstyle + ',\'' + cellid +'\');">';

回答by adeneo

Tada

多田

var element = document.createElement('div');

element.className = 'cancel';
element.addEventListener('click', function() {
    remove_game(game.id, cellstyle, cellid);
}, false);