Javascript 使用jQuery动态添加onClick事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12284168/
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
Adding onClick event dynamically using jQuery
提问by Claudio Delgado
Due to a plugin being used, I can't add the "onClick" attribute to the HTML form inputs like usual. A plugin is handling the forms part in my site and it doesn't give an option to do this automatically.
由于使用了插件,我无法像往常一样将“onClick”属性添加到 HTML 表单输入中。一个插件正在处理我网站中的表单部分,它没有提供自动执行此操作的选项。
Basically I have this input:
基本上我有这个输入:
<input type="text" id="bfCaptchaEntry" name="bfCaptchaEntry" style="">
I want to add an onClick to it with jQuery onload for it to be like this:
我想使用 jQuery onload 为其添加一个 onClick ,使其如下所示:
<input onClick="myfunction()" type="text" id="bfCaptchaEntry" name="bfCaptchaEntry" style="">
How do I go about doing this?
我该怎么做?
I know this might not be standard practice but seems like the easiest option to do in my situation.
我知道这可能不是标准做法,但在我的情况下似乎是最简单的选择。
I'm a newbie to jQuery so any help is very much appreciated.
我是 jQuery 的新手,所以非常感谢任何帮助。
回答by hunter
You can use the click
event and call your function or move your logic into the handler:
您可以使用该click
事件并调用您的函数或将您的逻辑移动到处理程序中:
$("#bfCaptchaEntry").click(function(){ myFunction(); });
You can use the click
event and set your function as the handler:
您可以使用该click
事件并将您的函数设置为处理程序:
$("#bfCaptchaEntry").click(myFunction);
.click()
。点击()
Bind an event handler to the "click" JavaScript event, or trigger that event on an element.
将事件处理程序绑定到“click”JavaScript 事件,或在元素上触发该事件。
You can use the on
event bound to "click"
and call your function or move your logic into the handler:
您可以使用on
绑定到的事件"click"
并调用您的函数或将您的逻辑移动到处理程序中:
$("#bfCaptchaEntry").on("click", function(){ myFunction(); });
You can use the on
event bound to "click"
and set your function as the handler:
您可以使用on
绑定到的事件"click"
并将您的函数设置为处理程序:
$("#bfCaptchaEntry").on("click", myFunction);
.on()
。在()
Attach an event handler function for one or more events to the selected elements.
将一个或多个事件的事件处理函数附加到所选元素。
回答by Ardalan Shahgholi
try this approach if you know your object client name ( it is not important that it is Button or TextBox )
如果您知道对象客户端名称,请尝试这种方法(它是 Button 还是 TextBox 并不重要)
$('#ButtonName').removeAttr('onclick');
$('#ButtonName').attr('onClick', 'FunctionName(this);');
try this ones if you want add onClick event to a server object with JQuery
如果您想使用 JQuery 将 onClick 事件添加到服务器对象,请尝试这些
$('#' + '<%= ButtonName.ClientID %>').removeAttr('onclick');
$('#' + '<%= ButtonName.ClientID %>').attr('onClick', 'FunctionName(this);');
回答by Selvakumar Arumugam
Try below approach,
尝试以下方法,
$('#bfCaptchaEntry').on('click', myfunction);
or in case jQuery is not an absolute necessaity then try below,
或者如果 jQuery 不是绝对必需品,请尝试以下方法,
document.getElementById('bfCaptchaEntry').onclick = myfunction;
However the above method has few drawbacks as it set onclick as a property rather than being registered as handler...
但是,上述方法几乎没有缺点,因为它将 onclick 设置为属性而不是注册为处理程序...
Read more on this post https://stackoverflow.com/a/6348597/297641
阅读这篇文章的更多信息https://stackoverflow.com/a/6348597/297641
回答by Parv Sharma
$("#bfCaptchaEntry").click(function(){
myFunction();
});
回答by Gabriel Jaime Sierra Rua
Or you can use an arrow function to define it:
或者您可以使用箭头函数来定义它:
$(document).ready(() => {
$('#bfCaptchaEntry').click(()=>{
});
});
For better browser support:
为了更好的浏览器支持:
$(document).ready(function() {
$('#bfCaptchaEntry').click(()=>{
});
});
回答by GDBxNS
let a = $("<a>bfCaptchaEntry</a>");
a.attr("onClick", "function(" + someParameter+ ")");