HTML DOM 可取消事件屬性與 HTML 事件相關聯,因為 JavaScript 可以對這些事件做出反應。可取消事件屬性返回布爾值 true 或 false,指示事件是否可以取消。
用法
以下是可取消事件屬性的語法 -
event.cancelable
示例
讓我們看一個可取消事件屬性的例子 -
<!DOCTYPE html>
<html>
<body>
<p>Hover over the button below to find out if onmouseover is cancellable event or not</p>
<button onmouseover="cancelFunction(event)">CLICK IT</button>
<p id="Sample"></p>
<script>
function cancelFunction(event) {
var x = event.cancelable;
if(x==true)
document.getElementById("Sample").innerHTML = "The onmouseover event is cancellable";
else
document.getElementById("Sample").innerHTML = "The onmouseover event is not
cancellable";
}
</script>
</body>
</html>
輸出
這將產生以下輸出 -
將鼠標懸停在“單擊它”按鈕上 -
我們首先創建了一個按鈕 CLICK IT,它將在鼠標懸停時將 ommouseover 事件對象傳遞給 cancelFunction(event) 方法。
<button onmouseover="cancelFunction(event)">CLICK IT</button>
cancelFunction(event) 方法檢查傳遞的事件對象的 event.cancelable 值並將其分配給變量 x。使用條件語句,我們檢查 event.cancellable 是否返回 true 或 false,然後在 id 等於 “Sample” 的段落標記中顯示合適的消息 -
function cancelFunction(event) { var x = event.cancelable; if(x==true) document.getElementById("Sample").innerHTML = "The onmouseover event is cancellable"; else document.getElementById("Sample").innerHTML = "The onmouseover event is not cancellable"; }
相關用法
- HTML DOM console.dirxml()用法及代碼示例
- HTML DOM console.count()用法及代碼示例
- HTML DOM console.log()用法及代碼示例
- HTML DOM contains()用法及代碼示例
- HTML DOM cloneNode()用法及代碼示例
- HTML DOM createElement()用法及代碼示例
- HTML DOM console.error()用法及代碼示例
- HTML DOM console.assert()用法及代碼示例
- HTML DOM createRange()用法及代碼示例
- HTML DOM console.clear()用法及代碼示例
- HTML DOM console.groupEnd()用法及代碼示例
- HTML DOM customElements get()用法及代碼示例
- HTML DOM customElements define()用法及代碼示例
- HTML DOM console.time()用法及代碼示例
- HTML DOM createDocumentType()用法及代碼示例
- HTML DOM createAttribute()用法及代碼示例
- HTML DOM console.group()用法及代碼示例
- HTML DOM click()用法及代碼示例
- HTML DOM createHTMLDocument()用法及代碼示例
- HTML DOM createObjectURL()用法及代碼示例
注:本文由純淨天空篩選整理自AmitDiwan大神的英文原創作品 HTML DOM cancelable Event Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。