HTML DOM select remove() 方法
htmlweb developmentfront end technology更新于 2025/8/8 0:44:17
HTML DOM select remove() 方法用于从 HTML 文档的下拉列表中删除一个新选项。
语法
以下是语法 −
object.remove(index)
其中,index 表示要从下拉列表中删除的选项的索引。
示例
让我们看一个 HTML DOM select remove() 方法的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
html{
height:100%;
}
body{
text-align:center;
color:#fff;
background: radial-gradient( circle farthest-corner at 23.1% 64.6%, rgba(129,125,254,1) 0%, rgba(111,167,254,1) 90% ) no-repeat;
height:100%;
}
p{
font-weight:700;
font-size:1.1rem;
}
.drop-down{
display:block;
width:35%;
border:2px solid #fff;
font-weight:bold;
padding:2px;
margin:1rem auto;
outline:none;
}
.btn{
background:orange;
border:none;
height:2rem;
border-radius:2px;
width:35%;
margin:2rem auto;
display:block;
color:#fff;
font-weight:bold;
outline:none;
cursor:pointer;
}
.show{
font-size:1.5rem;
font-weight:bold;
}
</style>
</head>
<body>
<h1>DOM Select remove() method Demo</h1>
<p>Hi, Select your favourite subject:</p>
<select class='drop-down' name="Drop Down List">
<option>Physics</option>
<option>Biology</option>
<option>Maths</option>
</select>
<button type="button" onclick="addSubject()" class="btn">Remove Physics Subject</button>
<div class="show"></div>
<script>
function addSubject() {
var dropDown = document.querySelector(".drop-down");
document.querySelector(".show").innerHTML="Removed Physics Subject";
dropDown.remove(0);
}
</script>
</body>
</html>
输出
这将产生以下输出 −
_method.jpg)
点击"删除物理学科"删除第一个学科,即下拉列表中当前的"物理学":
_method_ex.jpg)
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

