我想要從一個Ajax調用所有元素,然後將其插入另一個元素沒有檢索元素:使用jQuery如何從一個Ajax調用
- (我只想用純JavaScript)
- 創建一個新的元素來包含Ajax響應
這是我曾嘗試:
的index.php
<!DOCTYPE HTML>
<head>
<script type="text/javascript">
function loadPage() {
var ajax = new XMLHttpRequest();
ajax.open('GET', 'test.php', true);
ajax.onreadystatechange = function(){
if(ajax.readyState === 4 && ajax.status === 200){
document.getElementById('output').appendChild(ajax.responseText) ;
}
};
ajax.send();
}
loadPage();
</script>
</head>
<body>
<div id="output">
<h1>Default</h1>
</div>
</body>
</html>
test.php的
<h1>
its work
</h1>
<div>
<h2>
its work2
</h2>
</div>
我已經GOOGLE了它,但得到的答覆總是使用jQuery。
您在javascript中創建的每個元素都會返回對該節點的引用。但在ajax調用中沒有參考。所以你不能使用appendChild,而不是使用document.getElementById('output')。innerHTML = ajax.responseText; – Shadow
我不想抹殺元素 – ilike