使用原型来设置按钮的onclick事件?
问题描述:
我有这个按钮:使用原型来设置按钮的onclick事件?
<button id="check_button" name ="check_button" type="button" onclick="setLocation('...')">
<span>check</span>
</button>
现在我知道我可以用$( 'check_button'),例如访问它。有没有办法用Prototype设置setlocation参数?
谢谢!
答
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);
这里是为element.on
文档:http://api.prototypejs.org/dom/Element/prototype/on/
这实在是创建一个新的Event.Handler
的只是一个快速的方法:http://api.prototypejs.org/dom/Event/on/
为了确保检查出第二文档链接 - 超级有用的东西。
答
是,
$( 'check_button')观察( '点击',this.setLocation.bind(这一点, '...'));
setLocation: function(param)
{
alert(param);
}
这样帕拉姆将永远是你通过什么。 “绑定”是一种持有该能力的变化强大的方法。