How do I load html into a variable with jquery

前端 未结 4 1107
予麋鹿
予麋鹿 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:39

    While creating a new element is one option, you could also clone any element. This copies all the attributes and the values of the old Node, as it says, 'exact clone'.

    In case you want only a particular section of the html to be copied, this also provides the flexibility to fill all the contents within the particular element hierarchy (i.e., with all the children included) from the fetched page.

    For instance, if the hierarchy is -

    ...
    //... var oldElement = document.getElementById('mydiv'); var newElement = oldElement.cloneNode(true); /* #selector selects only that particular section & the '> *' enables to copy all of the child nodes under the parent #selector Replace URL with the required value function specification is optional... */ jQuery(newElement).load(URL+'#selector > *'[,function(response, status, xhr){}]); //...

    Now you can programatically process the variable newElement as you want (using native javascript as well, since it is a native element).

提交回复
热议问题