js下拉列表动态值的添加与删除问题,第一个函数没有问题,第二个函数哪里有问题?
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xh...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>插入和删除节点</title>
<style type="text/css">
</style>
<script type="text/javascript">
function addCity(){
var body = document.getElementsByTagName("body")[0];
var select = document.getElementsByTagName("select")[0];
var option = document.createElement("option");
var cityName = document.getElementById("cityName").value;
option.innerHTML = cityName;
select.appendChild(option);
}
function deleteCity(){
var option = document.getElementsByTagName("option");
var select = document.getElementsByTagName("select")[0];
for(var i=0;i<option.length;i++){
if(option[i].selected){
select.removeChild(option[i]);
}
}
</script>
</head>
<body>
<button onclick="addCity()">添加城市</button>请输入要添加的城市:
<input type="text" id="cityName" />
<select>
<option>成都</option>
</select>
<button onclick="deleteCity()">删除城市</button>
</body>
</html> 展开
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>插入和删除节点</title>
<style type="text/css">
</style>
<script type="text/javascript">
function addCity(){
var body = document.getElementsByTagName("body")[0];
var select = document.getElementsByTagName("select")[0];
var option = document.createElement("option");
var cityName = document.getElementById("cityName").value;
option.innerHTML = cityName;
select.appendChild(option);
}
function deleteCity(){
var option = document.getElementsByTagName("option");
var select = document.getElementsByTagName("select")[0];
for(var i=0;i<option.length;i++){
if(option[i].selected){
select.removeChild(option[i]);
}
}
</script>
</head>
<body>
<button onclick="addCity()">添加城市</button>请输入要添加的城市:
<input type="text" id="cityName" />
<select>
<option>成都</option>
</select>
<button onclick="deleteCity()">删除城市</button>
</body>
</html> 展开
1个回答
推荐于2016-06-09
展开全部
function deleteCity(){
var option = document.getElementsByTagName("option");
var select = document.getElementsByTagName("select")[0];
for(var i=0;i<option.length;i++){
if(option[i].selected){
select.removeChild(option[i]);
}
}
} // Error 缺少},函数都闭合,加载页面就报错了;自己多调试吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询