Jquery popover plugin colorbox binding method for dynamically generating elements

  • 2020-03-30 03:24:51
  • OfStack

Colorbox is a very useful popover plugin for jquery with rich functions and a good experience to use.

Colorbox website: http://www.jacklmoore.com/colorbox/


I just had a problem with colorbox that I hadn't noticed before.

I used to talk about this plugin in static HTML elements, but today it's not working when binding dynamically generated elements.

The usual usage goes like this:


<a class="test" href="test.jpg" title=" Welcome to script house ">www.jb51.net</a>


$(document).ready(function(){   
    $(".test").colorbox();  
});

The above method only applies to static elements. If you want to bind colorbox effects to dynamic elements, you should use the following method:

We're going to assume the < in the HTML above. A> Tags are dynamically generated elements, so js should write:


$(".test).live('click',function(){
    $.colorbox({href:$(this).attr('href'), open:true, height:'100%'});
    return false;
});


Related articles: