- jQuery 教程
- jQuery - 首頁
- jQuery - 路線圖
- jQuery - 概述
- jQuery - 基礎
- jQuery - 語法
- jQuery - 選擇器
- jQuery - 事件
- jQuery - 屬性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 新增元素
- jQuery - 刪除元素
- jQuery - 替換元素
- jQuery CSS 操作
- jQuery - CSS 類
- jQuery - 尺寸
- jQuery - CSS 屬性
- jQuery 效果
- jQuery - 效果
- jQuery - 動畫
- jQuery - 鏈式呼叫
- jQuery - 回撥函式
- jQuery 遍歷
- jQuery - 遍歷
- jQuery - 遍歷祖先
- jQuery - 遍歷後代
- jQuery UI
- jQuery - 互動
- jQuery - 小部件
- jQuery - 主題
- jQuery 參考
- jQuery - 選擇器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍歷
- jQuery - 其他
- jQuery - 屬性
- jQuery - 實用工具
- jQuery 外掛
- jQuery - 外掛
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用資源
- jQuery - 常見問題解答
- jQuery - 快速指南
- jQuery - 有用資源
- jQuery - 討論
jQuery 事件 off() 方法
jQuery 的 off() 方法用於移除使用 on() 方法附加到元素的事件處理程式。此方法還可用於從選定的元素中移除一個或多個事件處理程式,這些處理程式由事件型別、選擇器和函式指定。
在從元素中移除多個事件時,請確保事件值之間用空格分隔。
語法
以下是 jQuery 事件 off() 方法的語法:
$(selector).off(event, selector, function(eventObj), map);
引數
此方法接受四個引數,分別命名為“event”、“selector”、“handler”和“map”,如下所述:
- event - 指定要從元素中移除的一個或多個事件。
- selector (可選) - 用於篩選選定元素的後代(或子元素)的選擇器字串,這些後代將呼叫處理程式。
- function - 要移除的特定處理程式函式。
- map - 一個對映包含鍵值對,其中鍵指定事件,值指定相應的處理程式函式。
返回值
此方法沒有任何返回值。
示例 1
以下程式演示了 jQuery 事件 off() 方法的用法:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<style>
p{
font-size: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<p>Tutorialspoint (click to change color)</p>
<button>Remove event handler</button>
<script>
$('p').on("click", function(){
$(this).css("color", "green");
})
$('button').click(function(){
$('p').off("click");
alert("Eevnt removed...!");
})
</script>
</body>
</html>
輸出
執行程式後,將顯示一個 p 元素和一個按鈕。當用戶點選 p 元素時,其顏色將變為綠色,當點選按鈕時,事件處理程式將從“p”元素中移除:
示例 2
以下是 jQuery 事件 off() 方法的另一個示例。我們使用此方法來移除 <div> 元素點選事件的特定事件處理程式函式:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<style>
div{
background-color: green;
padding: 10px;
width: 300px;
}
button{
margin: 10px 0px;
padding: 10px 20px;
}
</style>
</head>
<body>
<p>Mouse over the below element</p>
<div>Tutorialspoint</div>
<button>Remove</button>
<span></span>
<script>
$('div').on("mouseover", custHandler);
function custHandler(){
alert("Handler for mouseover called.")
}
$('button').click(function(){
$('div').off("mouseover", custHandler);
$('span').text("Removed...!");
})
</script>
</body>
</html>
輸出
執行上述程式後,將顯示一個框(由 div 元素表示)和一個按鈕。當滑鼠指標懸停在 div 上時,瀏覽器螢幕上會出現一個彈出警報。當點選按鈕時,它會從 div 中移除特定的事件處理程式:
示例 3
在下面的示例中,我們使用 jQuery off() 方法從指定的元素中移除所有事件處理程式:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<style>
div{
padding: 10px;
width: 200px;
transition: 1s;
}
button{
margin: 10px 0px;
padding: 10px 20px;
}
</style>
</head>
<body>
<p>Click, double click, mouse enter, and mouse out from the below element</p>
<div>Tutorialspoint</div>
<button>Remove</button><span></span>
<span></span>
<script>
$('div').on("click dblclick mousenter mouseleave", function(){
$(this).css({"background-color": "green", "color": "white", "border-radius": "10px", "width": "300px"});
});
$('button').click(function(){
$('div').off();
});
</script>
</body>
</html>
輸出
執行上述程式後,將顯示一個 div 元素和一個按鈕。當點選按鈕時,它會從 div 元素中移除所有事件處理程式:
jquery_ref_events.htm
廣告
