Putting an ajax call results into a variable that can be manipulated as a div

时间:2015-07-31 20:00:39

标签: jquery ajax

I'm making an ajax call and get results back.

  $.get('/page.html',function(data){

      var myData = data;

    });

Is there a way that i can store the results in a variable and then manipulate that variable as if it was a container? If i make a change, something like this:

$('#myData ul').remove()

Now the myData variable still has the information but now with the ul removed?

1 个答案:

答案 0 :(得分:1)

Provided the data returned by ajax call is html, you can create the object using jQuery($) and treat it as DOM element to make modifications.

data = "<div> <h1> Sample header </h1> <ul> <li> List item 1 </li> <li> List item 2 </li> </ul>";
var myData = $(data);
myData.find('ul').remove();
$('body').append(myData);

Here is the JSFiddle: https://jsfiddle.net/ex0yuL7s/1/