Javascript onclick 函数自动运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14425397/
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
onclick function runs automatically
提问by T-L
Using google apps script I'm having trouble running a js function which passes parameters. When I add the parameters it will always run the code when the page loads instead of when the button is clicked.
使用谷歌应用程序脚本我在运行传递参数的 js 函数时遇到问题。当我添加参数时,它将始终在页面加载时运行代码,而不是在单击按钮时运行。
Direct from the HtmlService example, it is OK - it runs when the button is pressed...
直接从 HtmlService 示例来看,没问题 - 它在按下按钮时运行......
document.getElementById('button1').onclick = doSomething;
But when I add a parameter to the call (and function) as below, it runs just once when the page loads (and not when the button is pressed)...
但是当我向调用(和函数)添加一个参数时,如下所示,它在页面加载时(而不是在按下按钮时)只运行一次......
document.getElementById('button1').onclick = doSomething('with_this_parameter');
Any insight into this behaviour would be greatly appreciated... sorry if the answer is obvious!
对这种行为的任何见解将不胜感激......如果答案很明显,对不起!
回答by Hanky Panky
When you say
当你说
document.getElementById('button1').onclick = doSomething('with_this_parameter');
This means call doSomething('with_this_parameter')and then assign the returned value to document.getElementById('button1').onclick. Hence that is why it gets called when code reaches that line. Whether the value is assignable to that property or not is another question, but that is why it gets called.
这意味着调用doSomething('with_this_parameter')然后将返回的值分配给document.getElementById('button1').onclick。因此,这就是当代码到达该行时它被调用的原因。该值是否可分配给该属性是另一个问题,但这就是它被调用的原因。
Use it like this
像这样使用它
document.getElementById('button1').onclick = function(){
doSomething('with_this_parameter');
}
Reference: This solution was given by Mark Linus.
参考:此解决方案由Mark Linus给出。
回答by Danilo Valente
Do like this:
这样做:
document.getElementById('button1').onclick = function(){
doSomething('with_this_parameter');
}
回答by Trevor Iampen
I usually do clickHandlers like so:
我通常像这样执行 clickHandlers:
// create button here or get button...
var button1 = document.getElementById('button1').setName('button1');
var clickHandler = app.createServerClickHandler('doSomething');
button.addClickHandler(clickHandler);
function doSomething(e){
var button1 = e.parameter.button1;
<do something with var button>
}
I'm not sure what parameter you are adding, but you need to add a callback element to pass it if it isn't passed by the button itself via a .setId/getId or .setTag/getTag. If it is from a textbox:
我不确定您要添加什么参数,但是如果按钮本身没有通过 .setId/getId 或 .setTag/getTag 传递它,则需要添加一个回调元素来传递它。如果它来自文本框:
var textbox = app.createTextBox();
var button1 =
app.createButton.setName('button1');
var clickHandler =
app.createServerClickHandler('doSomething').addCallbackElement(textBox);
button1.addClickHandler(clickHandler);
Hope this helps!
希望这可以帮助!
回答by abhishek kasana
To assign a reference of function to some variable, you do:
要将函数的引用分配给某个变量,请执行以下操作:
var a = doSomething;
where doSomething is a function.
doSomething 是一个函数。
But when you have to pass parameters and assign that function
但是当您必须传递参数并分配该功能时
var a = doSomething(b);
this will cause trouble as while assigning the function to the variable, it gets called and not when it is intended to be called.
这会导致麻烦,因为在将函数分配给变量时,它会被调用,而不是在打算调用时调用。
To overcome this, you can use arrow functions or simple function to call your own function with params.
为了克服这个问题,您可以使用箭头函数或简单函数来调用您自己的带有参数的函数。
var c = () => doSomething(d);
This actually is understood as var c = anonymous_function;
这个其实理解为 var c =anonymous_function;
or
或者
var c = function() {
doSomething(d);
}
Hence you can do:
因此你可以这样做:
document.getElementById('button1').onclick = () => doSomething('with_this_parameter');

