How do I load html into a variable with jquery

前端 未结 4 1099
予麋鹿
予麋鹿 2020-12-04 19:23

I know I can load in html in to a div with:

$(\"#my_div\").load(\"http://www.mypage.com\");

but I want to do is load html into a variable l

4条回答
  •  半阙折子戏
    2020-12-04 19:49

    $.get("http://www.mypage.com", function( my_var ) {
        // my_var contains whatever that request returned
    });
    

    Underneath, jQuery will launch an ajax request which fires to the given URL. It also tries to intelligently guess which data is going to be received (if it's valid html you don't need to specify). If you need to get another data type just pass that in as last argument, for instance

    $.get("http://www.mypage.com", function( my_var ) {
        // my_var contains whatever that request returned
    }, 'html');  // or 'text', 'xml', 'more'
    

    Reference: http://api.jquery.com/jQuery.get/

提交回复
热议问题