- HTML 教程
- HTML - 首頁
- HTML - 路線圖
- HTML - 簡介
- HTML - 歷史與演變
- HTML - 編輯器
- HTML - 基本標籤
- HTML - 元素
- HTML - 屬性
- HTML - 標題
- HTML - 段落
- HTML - 字型
- HTML - 塊
- HTML - 樣式表
- HTML - 格式化
- HTML - 引用
- HTML - 註釋
- HTML - 顏色
- HTML - 圖片
- HTML - 圖片地圖
- HTML - 內聯框架
- HTML - 短語元素
- HTML - 元標籤
- HTML - 類
- HTML - ID
- HTML - 背景
- HTML 表格
- HTML - 表格
- HTML - 表格標題和說明
- HTML - 表格樣式
- HTML - 表格 Colgroup
- HTML - 巢狀表格
- HTML 列表
- HTML - 列表
- HTML - 無序列表
- HTML - 有序列表
- HTML - 定義列表
- HTML 連結
- HTML - 文字連結
- HTML - 圖片連結
- HTML - 郵件連結
- HTML 顏色名稱和值
- HTML - 顏色名稱
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML 表單
- HTML - 表單
- HTML - 表單屬性
- HTML - 表單控制元件
- HTML - 輸入屬性
- HTML 媒體
- HTML - 影片元素
- HTML - 音訊元素
- HTML - 嵌入多媒體
- HTML 頭部
- HTML - 頭元素
- HTML - 新增 Favicon
- HTML - Javascript
- HTML 佈局
- HTML - 佈局
- HTML - 佈局元素
- HTML - 使用 CSS 進行佈局
- HTML - 響應式設計
- HTML - 符號
- HTML - 表情符號
- HTML - 樣式指南
- HTML 圖形
- HTML - SVG
- HTML - Canvas
- HTML API
- HTML - Geolocation API
- HTML - 拖放 API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web 儲存
- HTML - 伺服器傳送事件
- HTML 雜項
- HTML - 文件物件模型 (DOM)
- HTML - MathML
- HTML - 微資料
- HTML - IndexedDB
- HTML - Web 訊息傳遞
- HTML - Web CORS
- HTML - Web RTC
- HTML 演示
- HTML - 音訊播放器
- HTML - 影片播放器
- HTML - 網頁幻燈片
- HTML 工具
- HTML - Velocity Draw
- HTML - 二維碼
- HTML - Modernizer
- HTML - 驗證
- HTML - 顏色拾取器
- HTML 參考
- HTML - 速查表
- HTML - 標籤參考
- HTML - 屬性參考
- HTML - 事件參考
- HTML - 字型參考
- HTML - ASCII 碼
- ASCII 碼錶查詢
- HTML - 顏色名稱
- HTML - 實體
- MIME 媒體型別
- HTML - URL 編碼
- 語言 ISO 程式碼
- HTML - 字元編碼
- HTML - 已棄用標籤
- HTML 資源
- HTML - 快速指南
- HTML - 有用資源
- HTML - 顏色程式碼生成器
- HTML - 線上編輯器
HTML - DOM querySelector() 方法
HTML DOM 的querySelector() 方法允許您選擇並訪問文件中與給定 CSS 選擇器匹配的第一個 HTML 元素。
CSS 選擇器用於查詢或選擇您想要設定樣式或操作的文件中的元素。例如,您可以使用 ID、類名和各種其他選擇器來定位特定元素。
語法
以下是 HTML DOM querySelector() 方法的語法:
element.querySelector(CSS_selectors);
引數
此方法接受如下所述的一個引數:
| 引數 | 描述 |
|---|---|
| CSS_selectors | 一個字串,定義您要選擇的元素型別。它可以是 ID、類、元素型別(如 <div> 或 <p>),甚至特定的屬性及其值。 |
返回值
此方法返回與給定 CSS 選擇器匹配的第一個 HTML 元素。如果未找到匹配的元素,則返回“null”。
示例 1:將 Highlight 類應用於選定元素
以下是 HTML DOM querySelector() 方法的基本示例。當單擊按鈕時,它會將“highlight”類新增到匹配的段落 (<p>) 元素:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM querySelector() Method</title>
<style>
.highlight {
background-color: lightblue;
}
button{
margin: 5px 0px;
padding: 8px;
}
</style>
</head>
<body>
<p>Selects the first element that matches the given CSS selector</p>
<h4>This is a heading</h4>
<p>This is a paragraph.</p>
<p class="hi">This is another paragraph with class="hi".</p>
<div>This is a div element.</div>
<button onclick="applyHighlight()">Apply Highlight</button>
<script>
function applyHighlight() {
// Selecting the element with class "hi"
const hele = document.querySelector('.hi');
// Applying bold font weight and highlight
hele.style.fontWeight = 'bold';
hele.classList.add('highlight');
// Creating a new paragraph to display the action
const resp = document.createElement('p');
resp.textContent = 'Applied bold font weight to element with ' + 'class "highlight".'+ '.highlight';
document.body.appendChild(resp);
}
</script>
</body>
</html>
示例 2:選擇並顯示元素文字
此示例演示了querySelector() 方法的使用,方法是選擇文件中的第一個 <p> 元素;然後它將該段落的文字內容顯示為輸出:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM querySelector() Method</title>
<style>
.highlight {
background-color: yellow;
}
button{
margin: 5px 0px;
padding: 8px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p class="highlight">This is another paragraph with class="highlight"</p>
<div>This is a div element.</div>
<button onclick="showOutput()">Show Output</button>
<script>
function showOutput() {
// Selecting the first <p> element
const firstP = document.querySelector('p');
// Creating a new paragraph to display the result
const resultP=document.createElement('p');
resultP.textContent = "Selected paragraph text: "+
"It selects the first element that matches " +
"the specified CSS selector ";
// Displaying the result in a clean way
document.body.appendChild(resultP);
}
</script>
</body>
</html>
示例 3:選擇和更改文字內容
在下面的示例中,querySelector() 方法選擇一個 <div> 元素,並在單擊按鈕時使用新的文字內容對其進行修改:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM querySelector() Method</title>
<style>
button{
margin: 5px 0px;
padding: 8px;
}
</style>
</head>
<body>
<p>Selecting and Changing Text Content</p>
<div id="con">Original text content.</div>
<button onclick="changeContent()">Change Content</button>
<script>
function changeContent() {
const cele = document.querySelector('#con');
// Changing text content of selected element
cele.textContent = 'Updated text content!';
// Creating a paragraph for displaying action
const resp = document.createElement('p');
resp.textContent =
'Changed text content of element with ID "con".';
// Displaying the result in a clean way
document.body.appendChild(resp);
}
</script>
</body>
</html>
示例 4:在選定元素上新增事件監聽器
以下示例顯示瞭如何使用querySelector() 方法選擇一個元素並動態新增事件監聽器:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM querySelector() Method</title>
<style>
button{
margin: 5px 0px;
padding: 8px;
}
</style>
</head>
<body>
<p>Adding Event Listener on selected element...</p>
<button id="myButton">Click Me!</button>
<div id="output"></div>
<script>
document.addEventListener('DOMContentLoaded',
function() {
const b=document.querySelector('#myButton');
const op = document.querySelector('#output');
b.addEventListener('click', function() {
output.innerHTML = '';
const msg = document.createElement('p');
msg.textContent = 'Button clicked!';
op.appendChild(msg);
});
});
</script>
</body>
</html>
示例 5:選擇和修改表單元素
此示例顯示了querySelector() 方法如何選擇表單元素並在輸出面板中顯示其值:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM querySelector() Method</title>
<style>
button{
margin: 5px 0px;
padding: 8px;
}
</style>
</head>
<body>
<p>Selects the form elements and modifies their values...</p>
<form id="myForm">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<br><br>
<button type="button"onclick="submitForm()">Submit Form</button>
</form>
<div id="output"></div>
<script>
function submitForm() {
const form=document.querySelector('#myForm');
const uin = form.querySelector('#username');
const pin = form.querySelector('#password');
// Displaying form data in the output panel
const op = document.querySelector('#output');
op.innerHTML = `<p>Username:
${uin.value}</p><p>Password:
${pin.value}</p>`;
}
</script>
</body>
</html>
示例 6:選擇和修改選定的表格行
在以下示例中,使用 querySelector() 方法修改表格行。它將“highlight”類新增到由年齡最小的人選擇的行:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM querySelector() Method</title>
<style>
button{
margin: 5px 0px;
padding: 8px;
}
.highlight {
background-color: yellow;
}
</style>
</head>
<body>
<p>Selects and adds highlight class on selected table row..</p>
<table id="myTable" border="1">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>25</td>
</tr>
<tr>
<td>Jane</td>
<td>30</td>
</tr>
</tbody>
</table>
<button onclick="highlightYoungest()">Highlight Youngest</button>
<script>
function highlightYoungest() {
const t=document.querySelector('#myTable');
const r = t.querySelectorAll('tbody tr');
// Finding the youngest person
let youngestAge = Infinity;
let youngestRow = null;
r.forEach(row => {
const age = parseInt(row.querySelector
('td:nth-child(2)').textContent);
if (age < youngestAge) {
youngestAge = age;
youngestRow = row;
}
});
youngestRow.classList.add('highlight');
//creates new paragraph to display the result
const rp = document.createElement('p');
rp.textContent="Highlighted the youngest " + "person (Age: ${youngestAge}) in the table.";
// Displaying the in a clean way
document.body.appendChild(rp);
}
</script>
</body>
</html>
示例 7:控制 Div 元素的可見性
下面的示例使用 querySelector() 方法控制元素的可見性。當單擊按鈕時,<div> 元素變為可見:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML DOM querySelector() Method</title>
<style>
.hidden {
display: none;
}
button{
margin: 5px 0px;
padding: 8px;
}
</style>
</head>
<body>
<p>Controls the visibility of a <div> element...</p>
<button onclick="toggleVisibility()">Control Visibility</button>
<div id="myDiv">
<p>Click the button to hide me!</p>
</div>
<script>
function toggleVisibility() {
const d=document.querySelector('#myDiv');
d.classList.toggle('hidden');
}
</script>
</body>
</html>
支援的瀏覽器
| 方法 | ![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|
| querySelector() | 是 4.0 | 是 8.0 | 是 3.5 | 是 3.2 | 是 10.0 |
html_dom_element_reference.htm
廣告




