jquery .load() page then parse html

前端 未结 3 903
长发绾君心
长发绾君心 2020-12-30 05:15

I have used the line below in my app. But now I need to parse the html loaded before I show it. Whats the best way to get certain html elements.

$(\"#div\").         


        
相关标签:
3条回答
  • 2020-12-30 05:40

    Instead of calling .load(), you should call $.get to perform an AJAX request and manually process the response.

    0 讨论(0)
  • 2020-12-30 05:49
    jQuery.ajax({
        url:'your_url',
        type:'get',
        dataType:'html',
        success:function(data)
       { 
           var _html= jQuery(data);
           //do some thing with html eg: _html.find('div').addClass('red')
           jQuery('#target').html(_html);
       }
    });
    
    0 讨论(0)
  • 2020-12-30 05:58

    You can use the semi-equivalent expanded version of $.get(), like this:

    $.get("page.html", function(data) {
      var data = $(data);
      //do something
      $("#div").html(data);
    });
    

    Note that calling .html(data) in this case is just a shortcut for .empty().append(data).


    Or, if post-processing is an option, just use the .load() callback, like this:

    $("#div").load("page.html", function() {
      $(this).find(".class").remove(); //just an example
    });
    
    0 讨论(0)
提交回复
热议问题