Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the event manipulation of jQuery on related controls

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to understand the event operation of jQuery on related controls, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Today, I suddenly became interested in his incident. I've encountered it before and haven't sorted it out. I'll do it when I'm free today.

For control events, jQuery has provided a wealth of methods, including binding, one-time binding, trigger, etc. Allah this morning to see where can be used to cut, big shrimp Lugu on it.

The binding event of jQuery is very convenient. There are bind, live, one and it helps you separate some commonly used events, such as the onclick event of the control. When we bind the onclick event, we only need to bind it.

$("# testButton") .click (function () {alert ("I'm Test Button");})

So we bind the onclick event to the button testButton to execute the alert statement. We can also use $("# testButton") .click (); to trigger this onclick event, and everything is very ok. The above is a bit sb, let's take a look at the cancellation event. JQuery has a unbind method for unbinding, that is, unbinding events, according to the above example, you should use: $("# testButton"). Unbind ("click"); well, it looks very good, if your click has two events, you can also use unbind ("click", fnName) to remove the binding of a specific function. Why is there a way to cancel a specific function? let's take a look at the example, we will find that the events of javascript are the same as those of C#, and the binding of events is superimposed (+ =) rather than overwritten.

Var Eat = function () {alert ("I want to eat");} var PayMoney = function () {alert ("pay in advance");} jQuery (document) .ready (function () {$("# testButton") .click (Eat); $("# testButton") .bind ("click", PayMoney);})

Using the above example, we find that it will pop up first: "I want to eat" followed by "pay in advance", indicating that its binding is done through onclick+=fn. Let's modify the method of ready:

JQuery (document) .ready (function () {$("# testButton") .click (Eat); $("# testButton") .unbind (); $("# testButton") .bind ("click", PayMoney);})

Another mistake, hehe, if you click the button this time, it will only execute PayMoney, not Eat, so if you put unbind () after bind, this button will not work. But what if I want to remove the bound PayMoney method? At this time, we should write:

JQuery (document) .ready (function () {$("# testButton") .click (Eat); $("# testButton") .bind ("click", PayMoney); $("# testButton") .unbind ("click", PayMoney);})

Hey, it's actually the same as bind, but next you will see a bug (I don't know if it counts). Let's have a close experience.

After reading the above, have you mastered how to understand jQuery's event manipulation on related controls? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report