Jquery替換已存在于element上的event
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-03-13 22:40:34 閱讀次數(shù):3453次
如何替換獲去除那些已存在于某個(gè)element上的事件呢? 看下面code:
1: <head>
2: <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
3: <title>TestPage</title>
4: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
5: <script type="text/javascript">
6: $(document).ready(function() {
7: $("#btn1").unbind('click').removeAttr('onclick').click(function() {
8: alert('The method has invoked by Jquery.');
9: });
10: });
11: function foo()
12: {
13: alert('The method has invoked.');
14: }
15: </script>
16: </head>
17:
18: <body>
19: <input type="button" value="ClickMe" id="btn1" onclick="foo()" />
20: </body>
21:
22: </html>
用的就是unbind,然后再removeAttr,最后再加上你的方法。來(lái)看下它們API document:
unbind([type], [fn])
概述
bind()的反向操作,從每一個(gè)匹配的元素中刪除綁定的事件。
如果沒(méi)有參數(shù),則刪除所有綁定的事件。
你可以將你用bind()注冊(cè)的自定義事件取消綁定。
如果提供了事件類(lèi)型作為參數(shù),則只刪除該類(lèi)型的綁定事件。
如果把在綁定時(shí)傳遞的處理函數(shù)作為第二個(gè)參數(shù),則只有這個(gè)特定的事件處理函數(shù)會(huì)被刪除。
參數(shù)
type (可選)String
事件類(lèi)型
fn (可選)Function
要從每個(gè)匹配元素的事件中反綁定的事件處理函數(shù)
removeAttr(name)
概述
從每一個(gè)匹配的元素中刪除一個(gè)屬性
參數(shù)
nameString
要?jiǎng)h除的屬性名
簡(jiǎn)單的就是:
1: $("#someelement").unbind('eventname').removeAttr('event').click(function() {
2: //your new method.
3: });
希望對(duì)您有幫助。
Author:Petter Liu http://wintersun.cnblogs.com
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)