将AJAX响应放入容器中,可以立即使用和操作

时间:2015-08-03 18:14:35

标签: javascript jquery ajax

我正在运行一个简单的ajax调用并获得返回的结果:

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

为了让我对信息做些什么,我必须将它放入div容器中。从那里我可以操纵数据。

$.get('/page.html',function(data){
  $('#results').html(data);
  $('#results').html().indexOf('^');
  $('#results ul').remove();
});

<div id="results"></div>

所以我的问题是,无论如何,我是否可以将响应数据放入其自己的容器中,而无需在页面上设置div?我尝试过这样的事情:

var myData = $(data);

但这不是一回事。做这样的事情:

myData.find('ul').remove();

有什么想法吗?所以问题是,我如何将响应放入一个我可以操作的变量中,并将其视为放在页面上的元素中?

来自我带来的页面的示例数据:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<!-- BC_OBNW -->

<head>

<title>TEST</title>

<link href="/StyleSheets/ModuleStyleSheets.css" type="text/css" rel="StyleSheet" />

<script type="text/javascript">var jslang='EN';</script>

<link rel="stylesheet" href="/CatalystStyles/Box.css?vs=b1722.r464991-phase1" type="text/css" media="screen" />

<script type="text/javascript" src="/CatalystScripts/Java_Box.js?vs=b1722.r464991-phase1"></script>

</head>

<body>

^,["C","A","0790","0603","PN","S","M","2","Standard","/krc-ea-ca07900603pnsm2"," CA07900603PNSM2","Doors","KRC-EA"],["C","A","0790","0609","PN","S","M","2","Standard","/krc-ea-ca07900609pnsm2"," CA07900609PNSM2","Doors","KRC-EA"],["C","A","0790","1203","PB","S","M","2","Standard","/krc-ea-ca07901203pbsm2"," CA07901203PBSM2","Doors","KRC-EA"],["C","A","0790","1203","PN","S","M","2","Standard","/krc-ea-ca07901203pnsm2"," CA07901203PNSM2","Doors","KRC-EA"],["C","A","0790","1209","PB","S","M","2","Standard","/krc-ea-ca07901209pbsm2"," CA07901209PBSM2","Doors","KRC-EA"],["C","A","0790","1209","PN","S","M","2","Standard","/krc-ea-ca07901209pnsm2"," CA07901209PNSM2","Doors","KRC-EA"],["C","A","1180","0603","PN","S","M","2","Standard","/krc-ea-ca11800603pnsm2"," CA11800603PNSM2","Doors","KRC-EA"],["C","A","1180","0609","PN","S","M","2","Standard","/krc-ea-ca11800609pnsm2"," CA11800609PNSM2","Doors","KRC-EA"],["C","A","1180","1203","PB","S","M","2","Standard","/krc-ea-ca11801203pbsm2"," CA11801203PBSM2","Doors","KRC-EA"],["C","A","1180","1203","PN","S","M","2","Standard","/krc-ea-ca11801203pnsm2"," CA11801203PNSM2","Doors","KRC-EA"],["C","A","1180","1209","PN","S","M","2","Standard","/krc-ea-ca11801209pnsm2"," CA11801209PNSM2","Doors","KRC-EA"],<ul id="webapp24958pagination" class="pagination webapp">

<li class="pag-current">1</li>

<li class="pag-number"><a href="/Default.aspx">2</a></li>

<li class="pag-next"><a href="/Default.aspx">Next</a></li>

</ul>



</body>

</html>

1 个答案:

答案 0 :(得分:0)

创建一个包装元素(未插入DOM)并从那里开始工作:

var myData = $('<div>').html(data);
myData.find('ul').remove();