mouseup 事件


將事件處理常式繫結到「mouseup」事件,或在元素上觸發該事件。

.on( "mouseup" [, eventData ], handler )傳回:jQuery

說明:將事件處理常式繫結到「mouseup」事件。

此頁面說明 mouseup 事件。如需已棄用的 .mouseup() 方法,請參閱 .mouseup()

當滑鼠指標在元素上方且滑鼠按鈕已釋放時,mouseup 事件會傳送到元素。任何 HTML 元素都可以接收此事件。

例如,考慮 HTML

1
2
3
4
5
6
<div id="target">
Click here
</div>
<div id="other">
Trigger the handler
</div>
圖 1 - 已呈現 HTML 的說明

事件處理常式可繫結到任何 <div>

1
2
3
$( "#target" ).on( "mouseup", function() {
alert( "Handler for `mouseup` called." );
} );

現在如果我們按一下此元素,會顯示警示

已呼叫 `mouseup` 處理常式。

當按一下不同的元素時,我們也可以觸發事件

1
2
3
$( "#other" ).on( "click", function() {
$( "#target" ).trigger( "mouseup" );
} );

此程式碼執行後,按一下 觸發處理常式 也會顯示警示訊息。

如果使用者按一下元素外側,拖曳到元素上,然後放開按鈕,這仍會計為 mouseup 事件。在大部分使用者介面中,這串動作不會被視為按鈕按壓,因此通常最好使用 click 事件,除非我們知道在特定情況下 mouseup 事件較佳。

範例

在觸發 mouseup 和 mousedown 事件時顯示文字。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>on demo</title>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<p>Press mouse and release here.</p>
<script>
$( "p" )
.on( "mouseup", function() {
$( this ).append( "<span style='color:#f00;'>Mouse up.</span>" );
} )
.on( "mousedown", function() {
$( this ).append( "<span style='color:#00f;'>Mouse down.</span>" );
} );
</script>
</body>
</html>

示範