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 does jQuery solve the problem of not executing the original event after adding an element

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "jQuery how to solve the problem of not executing the original event after adding elements". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "jQuery how to solve the problem of not executing the original event after adding elements"!

Let's look at my error code first.

html:

modify delete added

js:

$(".add").click(function(){ var newYuansu = $("delete"); $(".last").before(newYuansu);})$(".del").click(function(){ $(this).parents("tr").remove();})

At this time, jQuery is used to add new elements. The new elements will not execute some of the original event functions.

The blogger is a student and has only studied one method:

$("#best").on("click",".del",function(){ $(this).parents("tr").remove();})

Write the original click event like this, you can use it in the newly added element, very easy to use ~

Additional:

For jquery versions below 1.3 (excluding jquery 1.3), it's time to update your jquery version.

Because there's no solution, no solution.

2. If jquery version is between 1.3-1.8, js/jq dynamically added elements trigger binding event resolution (on event is not recommended, because on event is not supported below version 1.6, error will be reported)

Binding live events (live events are only supported in jquery1.9 and below, not in higher versions).

$(".del").live("click",function(){ ///jquery 1.9(excluding 1.9) The following can be alert ('this is an event added by dynamic elements'); })

3. Recommended use of on

If jquery version is 1.9 or higher, live delegate events are removed and implemented by on. js/jq Dynamically added elements trigger binding event resolution

Note: If the page has both a lower version of jq (1.3-1.8) and a higher version of jq (jquery1.9 or above), the live delegate event will be removed by the higher version, resulting in an error page even though there are jquery versions between 1.3-1.8 that use live events.

At this point, I believe that everyone has a deeper understanding of "jQuery how to solve the problem of not executing the original event after adding elements," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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