开发者

Problem to call/use .js file (jquery function)

I have this jq function to open a link inside a div:

$(document).ready(function() {
    $('#all').delegate('a', 'click' (function() {
        $('#content').empty();
        var page = $(this).attr("id");
        $('#content').load("scripts/loop_a.php?page=" + page);
    });
});

I call this inside the开发者_C百科 head tag of my index.php and is working for all links that are in my header_template.php

If I load a new.php page inside my main_body div and it has a link inside, that link does not work with my script, (basically my new.php page doesn't see my .js file).

Is there a way to make it work for any new .php file that I open by calling the .js file only in my index.php?

Thank you

Edit... Solution Found... I edided the script above.

In my index.php I am using which includes everything that happens. So I just chose that div and I delegate all the functions that I needed to work when loading/opening new.php files.

Thank you all for your help.


You may need a live event

$(document).ready(function() {
    $('.trgt_div > a').live('click', function() {
        $('#content').empty();
        var page = $(this).attr("id");
        $('#content').load("scripts/loop_a.php?page=" + page);
    });
});

Hope it helps


Elements injected via ajax need to have event handlers rebinded to them, or alternatively event handlers delegated to a suitable parent/container:

$('#content').delegate("a", "click", function(e) { 
    e.preventDefault(); // perhaps you need this?
    $('#content').empty();
    var page = $(this).attr("id");
    $('#content').load("scripts/loop_a.php?page="+ page);      
}); 

See:

  • http://api.jquery.com/delegate/
  • http://www.sitepoint.com/javascript-event-delegation-is-easier-than-you-think/

Note: if you're using < jQuery 1.4.2, you will need to use .live instead.


$(document).ready(function() { 
    $('#all').delegate('a', 'click' (function() { 
        $('#content').empty(); 
        var page = $(this).attr("id"); 
        $('#content').load("scripts/loop_a.php?page=" + page); 
    }); 
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜