HTML DOM 選項文字屬性
HTML DOM option text 屬性返回並修改 HTML 文件中的選項文字。
語法
語法如下 −
1. 返回文字
object.text
2. 修改文字
object.text = “text”
例
讓我們看看 HTML DOM option text 屬性的一個示例 −
<!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 Option text Property Demo</h1>
<p>Hi, Select your favourite subject −</p>
<select class='drop-down'>
<option>Physics</option>
<option>Biology<option>
<option>Maths<option>
</select>
<button type="button" onclick="changePhysicsName()" class="btn">Change Physics
Subject Name</button>
<div class="show"></div>
<script>
function changePhysicsName() {
document.querySelector(".drop-down").options[0].text="Applied Physics";
document.querySelector(".show").innerHTML="Now Physics become Applied Physics";
}
</script>
</body>
</html>輸出
該例會生成以下輸出 −

點選“更改物理學科名稱”按鈕以更改物理選項文字。

廣告
資料結構
網路技術
關係型資料庫管理系統
作業系統
Java
iOS
超文字標記語言
層疊樣式表
安卓
Python
C 語言
C++
C#
MongoDB
MySQL
JavaScript
PHP