使用jQuery动态添加onClick事件 [英] Adding onClick event dynamically using jQuery

查看:359
本文介绍了使用jQuery动态添加onClick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于使用了插件,我无法像往常一样将onClick属性添加到HTML表单输入中。
插件正在处理我网站中的表单部分,并且它没有给出自动执行此操作的选项。



基本上我有这样的输入: p>

 < input type =textid =bfCaptchaEntryname =bfCaptchaEntrystyle => 

我想用jQuery onload为它添加一个onClick,它就像这样:

 < input onClick =myfunction()type =textid =bfCaptchaEntryname =bfCaptchaEntrystyle = > 

我该如何去做这件事?

我知道这可能不是标准做法,但似乎是在我的情况下最简单的选择。



我是jQuery的新手,所以任何帮助都非常多赞赏。

解决方案您可以使用点击事件并调用您的函数或者将你的逻辑移入处理程序:

  $(#bfCaptchaEntry)。click(function(){myFunction(); }); 

您可以使用点击事件并设置您的函数作为处理函数:

  $(#bfCaptchaEntry)。click(myFunction); 






.click()


将事件处理程序绑定到clickJavaScript事件,或在元素上触发该事件。 b

http://api.jquery.com/click/






您可以使用事件绑定到click然后调用你的函数或者将你的逻辑移入处理函数:

  $(#bfCaptchaEntry)。on(click,function(){myFunction();}); 

您可以使用事件绑定的 click并将您的函数设置为处理函数:

  $( #bfCaptchaEntry)。on(click,myFunction); 






.on()


将一个或多个事件的事件处理函数附加到
选定的元素。


http://api.jquery.com/on/

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.

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:

<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.

解决方案

You can use the click event and call your function or move your logic into the handler:

$("#bfCaptchaEntry").click(function(){ myFunction(); });

You can use the click event and set your function as the handler:

$("#bfCaptchaEntry").click(myFunction);


.click()

Bind an event handler to the "click" JavaScript event, or trigger that event on an element.

http://api.jquery.com/click/


You can use the on event bound to "click" and call your function or move your logic into the handler:

$("#bfCaptchaEntry").on("click", function(){ myFunction(); });

You can use the on event bound to "click" and set your function as the handler:

$("#bfCaptchaEntry").on("click", myFunction);


.on()

Attach an event handler function for one or more events to the selected elements.

http://api.jquery.com/on/

这篇关于使用jQuery动态添加onClick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆