2011-11-04 59 views
0
function somefunction() 
{ 
var xmlhttp; 

if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 

xmlhttp.onreadystatechange=function() 
{ 
if (xmlhttp.readyState==4 && xmlhttp.status==200) 
{ 
alert(xmlhttp.responseText.getElementById('someid').innerHTML); 
} 
} 
xmlhttp.open("GET","1.php",true); 
xmlhttp.send(); 
} 

从XMLHTTP反应是这个是否有可能从responsetext获取元素,而不是先打印出来?

<p id="someid">100</p> 

我需要得到 “100” 出来自ID为 “someid”

使用该

alert(xmlhttp.responseText.getElementById('someid').innerHTML); 

,但它不试图没有工作。

还有另外一种方法吗?

在此先感谢

回答

0

在jQuery中就可以了,

$.ajax({ 
type: "GET", 
url: "1.php", 
contentType: "application/json; charset=utf-8", 
dataType: "json", 
success: function(response) { 
    alert($('#SomeID',response.d)); 
    alert($('#SomeID',$('<div/>').html(response.d))); 
}, 
failure: function(msg) { 
    alert(msg); 
} 
相关问题