1
<html>
2
<head>
3
<title> Demo </title>
4
<script language=javascript>
5
function deloption()
{
6
var obj=document.getElementById("sel").options;
7
8
if (obj.length > 1)
{
9
obj.options.remove(obj.length - 1);
10
}
11
}
12
13
function addoption()
14

{
15
var obj=document.getElementById("sel").options;
16
var opt = new Option("newoption" + obj.length, obj.length );
17
obj.options.add(opt);
18
19
}
20
</script>
21
</head>
22
<body>
23
<select name="sel">
24
<option>one
25
</select>
26
<input type="button" onclick="addoption()" value="ADD">
27
<input type="button" onclick="deloption()" value="DEL">
28
</body>
29
</html>
<html>2
<head>3
<title> Demo </title>4
<script language=javascript>5

function deloption()
{ 6
var obj=document.getElementById("sel").options; 7

8

if (obj.length > 1)
{9
obj.options.remove(obj.length - 1); 10
} 11
} 12

13
function addoption() 14


{15
var obj=document.getElementById("sel").options; 16
var opt = new Option("newoption" + obj.length, obj.length ); 17
obj.options.add(opt); 18

19
}20
</script>21
</head>22
<body>23
<select name="sel">24
<option>one25
</select>26
<input type="button" onclick="addoption()" value="ADD">27
<input type="button" onclick="deloption()" value="DEL">28
</body>29
</html>
本文介绍了一种使用JavaScript动态地向HTML下拉菜单中添加和删除选项的方法。通过两个按钮实现选项的增加和移除功能,展示了如何利用JavaScript操作DOM元素。
493

被折叠的 条评论
为什么被折叠?



