HTMLElement:click() 方法
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
HTMLElement.click() 方法會模擬滑鼠點擊一個元素。當在一個元素上呼叫此方法時,會觸發該元素的 click 事件(除非該元素的 disabled 屬性已被設定)。
語法
js
click()
參數
無。
回傳值
無(undefined)。
範例
當滑鼠游標移動到核取方塊上方時,模擬滑鼠點擊:
HTML
html
<form>
<input type="checkbox" id="myCheck" />
</form>
JavaScript
js
const checkbox = document.getElementById("myCheck");
// 當滑鼠移入時,執行 myFunction
checkbox.addEventListener("mouseover", () => {
// 模擬滑鼠點擊
checkbox.click();
});
checkbox.addEventListener("click", () => {
console.log("發生 click 事件");
});
規範
| Specification |
|---|
| HTML> # dom-click-dev> |