开发者

Needed help with jquery and ajax

Guys so I solved the problem of how to load a plugin after an ajax call, but now I am workin开发者_如何转开发g on how to load a plugin after an ajax call inside another ajax call. The ajax part functions perfectly but the plugin is not loading. Here is the code:

   $(document).ready(function () {

       var container = $("#content");

       var link;
       $("a.links").click(function (event) {

           event.preventDefault();
           link = $(this).attr("href") + " #content";
           var link2 = $(this).attr("href");
           container.load(link, function () {

               if (link2 == 'gallery.html') {
                   $("a.glinks").click(function (event) {
                       event.preventDefault();
                       link2 = $(this).attr("href") + " #content";
                       $("#content2").load(link2, function () {

                           var link3 = $("<link>");
                           link3.attr({
                               type: 'text/css',
                               rel: 'stylesheet',
                               href: 'C:\wamp\www\css\jquery.lightbox-0.5.css'
                           });
                           $("head").append(link3);

                           $('#gallery a').lightBox();
                       });
                   });
               }
           });
       });

I also tried to reload the stylesheet again.


Well, you really don't need any of this nesting.

Just load the stylesheet from original document. You don't need to load it dynamically:

<link type="text/css" rel="stylesheet" media="all" href="C:\wamp\www\css\jquery.lightbox-0.5.css">  

Note: it would be best if the href was a relative path.

Now, condense the JS to just one live click :

$(document).ready(function () {

   $("a.links, a.glinks").live('click', function (event) {
       event.preventDefault();
       var link = $(this).attr("href") + " #content";
       var target_div = $(this).hasClass('glinks') ? "#content2" : "#content";

       $(target_div).load(link, function () {
           $(this).find('#gallery a').lightBox(); 
       });
   });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜