mah*_*wde 2 html javascript ajax html-parsing
我使用 Javascript 在我的代码中使用 AJAX 调用。
function loadFacility(callback)
{
//alert('In loadFacility');
var xmlhttp;
var keys=document.firstCallInformation.facilityselect.value;
var urls="http://localhost:8080/webfdms/showFirstCallInformation.do?vitalsId=366";
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)
{
//var some=xmlhttp.responseXML.documentElement;
var response = xmlhttp.responseText;
console.log(response)
callback(xmlhttp.responseText);
}
}
xmlhttp.open("GET",urls,true);
xmlhttp.send(null);
}
function loadFacilityCallback(response){
if(response != null){
//alert(response);
console.log(response);
var div = document.createElement("div");
div.innerHTML = response;
document.getElementById("facilityselect").innerHTML = div.querySelectorAll("select#facilityselect");;
}
Run Code Online (Sandbox Code Playgroud)
编辑: 我已经更新了我的回调函数。但是在这里我收到了选择列表作为 [Object Nodelist]。现在如何在我的 HTML 中显示?
在回调函数中,我现在收到了 HTML 响应,我想解析该 HTML 响应,以便我可以进一步处理它。我正在使用普通的 javascript 来做到这一点。如何解析作为 HTML 接收的 ajax 响应?
创建一个DIV元素并将 HTML 放入其中innerHTML。那将解析它。
var div = document.createElement("div");
div.innerHTML = response;
Run Code Online (Sandbox Code Playgroud)
现在您可以在 中处理它div,例如div.querySelector(".classname")。要获取所有<select>标签,请执行以下操作:
var selects = div.querySelectorAll("select");
Run Code Online (Sandbox Code Playgroud)
要将其放入您的网页,您可以执行以下操作:
document.getElementById("facilityselect").innerHTML = div.querySelector("select#facilityselect").innerHTML
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3557 次 |
| 最近记录: |