开发者

Jquery load(): body class not retrieved

开发者 https://www.devze.com 2023-03-05 10:14 出处:网络
Wh开发者_如何学JAVAen I use jquery\'s .load() function to load the next pages body it does not retrieve the class of the body of that page. Is there a way to achieve this?

Wh开发者_如何学JAVAen I use jquery's .load() function to load the next pages body it does not retrieve the class of the body of that page. Is there a way to achieve this?

Thank you :)

Here is the code:

$(document).ready(function () {
    if(navigator.standalone){
        $('a').live("click", function (e) {
            var link = $(this).attr('href');
            if ($(this).hasClass('noeffect')){
            }
        else {
            e.preventDefault();
            $("body").load(link);
            };
        });
    }
    else {
        window.scrollTo(0, 0.9);
    }
});


so your putting a body inside a body? not sure that'll work as expected!

some html would be helpful, but i'm guessing that is what the problem is.

in the pages that you are loading into your body, put them inside a div instead of a body tag. that should avoid the body inside a body problem.


Edit:

what you could do is do a regular ajax call to get your new html. then remove your current body, and append the stuff you get back from the ajax call to your html. I've never tried something like that, but if you can't change the html that your getting back, it's probably the best thing to try.


You could try using replaceWith:

$(document).ready(function () {
    if(navigator.standalone){
        $('a').live("click", function (e) {
            var link = $(this).attr('href');
            if ($(this).hasClass('noeffect')){}
            else {
                e.preventDefault();
                $.get(link, null, function(result) {
                     $("body").replaceWith(result);
                });                    
            });
        }
    else {window.scrollTo(0, 0.9);}
});

This assumes that the response only contains the body and it's contents. If it includes the html you might want $("html").replaceWith(result)


I find a pretty easy solution to get the body element of ajax response. The idea is:

  1. Turn the BODY tag of response into a DIV
  2. Wrap the DIV with a BODY

    $.get(url)
      .success(function(data, textStatus, jqXHR){
        var data = data.replace('<body', '<body><div id="body"').replace('</body>', '</div></body>');
        var body = $(data).filter('#body');
        // feel free the get HTML, class, attributes, etc. of ajax response BODY
      });
    

It works in Chrome, so I believe in works in other browser as well.

0

精彩评论

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