Home  >  Article  >  Web Front-end  >  Solve the binding problem of events after Jquery appends new elements to the page_jquery

Solve the binding problem of events after Jquery appends new elements to the page_jquery

WBOY
WBOYOriginal
2016-05-16 16:09:16968browse

I first looked at the jq api documentation but couldn't find a method. To no avail, I had to look for some information online, and sure enough I found a live method.

It’s actually very simple:

1. This is the effect required by the project. When I did not use the live event and only used a simple hover event: the hover event was not loaded and there was no border effect I wanted. The rendering is as follows

Copy code The code is as follows:

/*Add styles through user skill tags*/
  $(function(){  
   $(".s-edited").hover(function(){ 
​​​​​$(this).toggleClass("borderd");
   })
  }) 

2. After I used live, the effect was achieved, as shown below:

The code is as follows:

Copy code The code is as follows:

/*Add styles through user skill tags*/
  $(".s-edited").live("hover",function(){
   $(this).toggleClass("borderd");
  })

I carefully read the introduction about live,

Bind an event handler (such as hover event) to all current and future matching elements. Custom events can also be bound.

PS: I am a newbie, please don’t criticize!

The above is the entire content of this article, I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn