javascript 使用原型设置按钮的 onclick 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10227402/
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
Using prototype to set an onclick event of a button?
提问by EOB
I have this button:
我有这个按钮:
<button id="check_button" name ="check_button" type="button" onclick="setLocation('...')">
<span>check</span>
</button>
Now I know I can access it using $('check_button')
for example. Is there a way to set the setlocation
parameter with Prototype?
现在我知道我可以使用$('check_button')
例如访问它。有没有办法setlocation
用Prototype设置参数?
回答by dontGoPlastic
function doButtonClick(event, element) {
console.log('event');
console.log('element');
// here, `element` is a reference to the button you clicked.
var elementId = element.identify();
// I'm not sure what setLocation takes in as a parameter, but...
setLocation(elementId);
}
$('check_button').on('click', 'button', doButtonClick);
Here are the docs for the element.on
: http://api.prototypejs.org/dom/Element/prototype/on/
以下是以下文档element.on
:http: //api.prototypejs.org/dom/Element/prototype/on/
Which is really just a quick way of creating a new Event.Handler
: http://api.prototypejs.org/dom/Event/on/
这实际上只是创建一个新的快速方法Event.Handler
:http: //api.prototypejs.org/dom/Event/on/
For sure check out that second doc link - super useful stuff.
一定要查看第二个文档链接 - 超级有用的东西。
回答by Ebin John
Yes,
是的,
$('check_button').observe('click',this.setLocation.bind(this,'...'));
setLocation: function(param)
{
alert(param);
}
In this way the param will be always what you pass. " bind " ia a vary powerfull method which holds that ability.
通过这种方式,参数将始终是您传递的内容。“绑定”是一种拥有这种能力的强大方法。