当先锋百科网

首页 1 2 3 4 5 6 7
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<button class="click">点击</button>
<div class="delete" οnclick="test2()">删除</div>
<script type="text/javascript">
var test = function(){
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}else{
xmlHttp = new ActiveXObject();
}
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
var str = "<select>";
var obj = JSON.parse(xmlHttp.responseText);
// var element;
for(let index = 0; index < obj.length; index++){
// element = obj[index];
str += "<option>" + obj[index].name +"</option>";
}
str += "</select>";
let divContain = document.createElement("div"); //创建div
divContain.innerHTML = str; //给div填充内容
document.body.appendChild(divContain); //将div插入body中
}

}
xmlHttp.open("GET", "http://localhost/hoby.json", true);
xmlHttp.send();
}

let btn = document.getElementsByClassName("click")[0];
btn.onclick = test;
function test2(){
var objArr = document.getElementsByTagName("select");
var elem = objArr[0];
elem.removeChild(elem.childNodes[1]);
}
</script>
</body>
</html>