[英]Changing the onclick event delegate of an HTML button?
How do you change the JavaScript that will execute when a form button is clicked? 如何更改单击表单按钮时将执行的JavaScript?
I've tried changing its onClicked and its onclicked child attributes like so: 我试过更改它的onClicked及其onclicked子属性,如下所示:
$('mybutton').onClick = 'doSomething';
and 和
$('mybutton').attributes["onclick"] = 'doSomething()';
Neither seem to work. 似乎都没有用。 My other options are:
我的其他选择是:
Neither seem very elegant. 看起来都不优雅。
I'm using Prototype as a js library so it that has any useful tools I can use them. 我正在使用Prototype作为js库,所以它有任何有用的工具我可以使用它们。
如果原始onclick事件是通过HTML属性设置的,则可以使用以下内容覆盖它:
$("#myButtonId").setAttribute("onclick", "myFunction();");
For Prototype, I believe that it would be something like this: 对于Prototype,我相信它会是这样的:
$("mybutton").observe('click', function() {
// do something here
});
EDIT: Or, as it says in the documentation , you could simply specify the function you want to call on click: 编辑:或者,正如文档中所述 ,您只需指定要在单击时调用的函数:
$('mybutton').observe('click', respondToClick);
function respondToClick(event) {
// do something here
}
But this is all, again, Prototype-specific. 但这又是原型特有的。
Using the Prototype framework you can do: 使用Prototype框架,您可以:
Event.observe("mybutton", "click", clickHandler);
or: 要么:
Event.observe("mybutton", "click", function() {
alert("Button clicked!");
});
or: 要么:
$("mybutton").observe("click", clickHandler);
or: 要么:
$("mybutton").observe("click", function() {
alert("Button clicked!");
});
The general way to set an onclick handler in javascript is to set onclick
to a function, by passing it the name of a function directly, not in a string. 设置在JavaScript的onclick处理的一般方法是设置
onclick
的功能,通过直接传递给它的名称的功能,而不是一个字符串。 So if myButton
is set to a DOM Element, you would write: 因此,如果将
myButton
设置为DOM元素,您可以编写:
myButton.onclick = doSomething;
So when you click the 'mybutton' button, the doSomething
function will be called as doSomething()
. 因此,当您单击“mybutton”按钮时,
doSomething
函数将被称为doSomething()
。 For anonymous functions, you can write: 对于匿名函数,您可以编写:
myButton.onclick = function() {
alert("myButton was clicked!");
};
I found a solution for your issue with prototype under firefox: 我在firefox下找到了原型问题的解决方案:
$("#myButtonId").writeAttribute('onclick', ''); // first remove the attribute
$("#myButtonId").observe('click', function () { ... }); // then add the event
In JQuery it's 在JQuery中它是
$("#myButtonId").click(myFunction);
function myFunction(){
alert("Clicked");
}
Or if you want to put the function inline: 或者如果你想把函数内联:
$("#myButtonId").click(function(){
alert("Clicked");
});
If you are using JQuery firstly make sure you use the relevant selector prefix (IE: If your using the Id of the element put a # in front of it). 如果您正在使用JQuery,请首先确保使用相关的选择器前缀(IE:如果您使用元素的ID在其前面放置一个#)。 Secondly it's the click method to assign a callback to the click event.
其次是click方法为click事件分配回调。
Last I used Prototype, it was something like this: 最后我使用Prototype,它是这样的:
Event.observe('mybutton', 'click', doSomething);
By the way, your examples might've even worked if you didn't quote the function names. 顺便说一句,如果您没有引用函数名称,您的示例可能甚至可以工作。
EDIT: Yes, Element.observe(element, eventName, handler) and someElement.observe(eventName, handler) also work. 编辑:是的,Element.observe(element,eventName,handler)和someElement.observe(eventName,handler)也可以。 And don't quote the handler name - you want to pass the function not a string!
并且不要引用处理程序名称 - 您希望传递函数而不是字符串!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.