鼠標事件:javascript
dblclick() 雙擊事件css
能夠傳遞參數,或者不傳遞$ele.click( [eventData ], handler(eventObject) )html
$("#test").click(11111,function(e) { //this指向 div元素 //e.data => 11111 傳遞數據 });
用event 對象的which區別按鍵,敲擊鼠標左鍵which的值是1,敲擊鼠標中鍵which的值是2,敲擊鼠標右鍵which的值是3java
mouseup與mousedown組合起來就是click事件jquery
$("#test").mousedown(11111,function(e) { //this指向 div元素 //e.data => 11111 傳遞數據 });
$(".aaron1").mousemove(function(e) { $(this).find('p:last').html('移動的X位置:' + e.pageX) })
var n = 0; //綁定一個mouseover事件 $(".aaron1 p:first").mouseover(function(e) { $(".aaron1 a").html('進入元素內部,mouseover事件觸發次數:' + (++n)) })
<div id="test">點擊觸發<div>
$("#test").mouseover(11111,function(e) { //this指向 div元素 //e.data => 11111 傳遞數據 });
mouseenter事件只會在綁定它的元素上被調用,而不會在後代節點上被觸發
$(".aaron2 p").mouseenter(function(e) { $(".aaron2 a:first").html('mouseenter事件觸發次數:' + (++i)) })
$(selector).hover(handlerIn, handlerOut)
handlerIn(eventObject):當鼠標指針進入元素時觸發執行的事件函數瀏覽器
handlerOut(eventObject):當鼠標指針離開元素時觸發執行的事件函數dom
// hover()方法是同時綁定 mouseenter和 mouseleave事件。 // 咱們能夠用它來簡單地應用在 鼠標在元素上行爲 $("p").hover( function() { $(this).css("background", 'red'); }, function() { $(this).css("background", 'yellow'); } );
//input聚焦 //給input元素增長一個邊框 $("input:first").focusin(function() { $(this).css('border','2px solid red') })
表單事件ide
//點擊裏面包含的元素纔有做用,寫成focus沒有用
$(".aaron").focusin(function() { $(this).css('border', '2px solid red') })
//監聽input值的改變 $('.target1').change(function(e) { $("#result").html(e.target.value) });
//監聽textarea元素中value的選中 $('textarea').select(function(e) { alert(window.getSelection().toString());//非IE內核瀏覽器能夠用這種方法,考慮兼容性的話你能夠用getselectionstart()和getselectionend() });
//alert(e.target.value.substring(e.currentTarget.selectionStart,e.currentTarget.selectionEnd));
具體能觸發submit事件的行爲:函數
<input type="submit">this
<input type="image">
<button type="submit">
當某些表單元素獲取焦點時,敲擊Enter(回車鍵)
$('#target2').submit(function() { alert('捕獲提交表達動做,阻止頁面跳轉') return false; });
鍵盤事件
//監聽鍵盤按鍵 //獲取輸入的值 $('.target1').keydown(function(e) { $("em:first").text(e.target.value) });
keydown 是在按以前判斷(當時text尚未內容),觸發一次空,當下一次按鍵時,就觸發上一次輸入的內容,因此就少一個。按下的一瞬間時 文字尚未輸入到文本框因此第一次下邊獲取不到這個值。簡單點說文字輸入進去文本框這個行爲要慢於keydown事件。
$("#elem").click(function(){}) //快捷方式 $("#elem").on('click',function(){}) //on方式
事件機制委託的機制
事件對象,e.target
event.target
target 屬性能夠是註冊事件時的元素,或者它的子元素。一般用於比較 event.target 和 this 來肯定事件是否是因爲冒泡而觸發的。常常用於事件冒泡時處理事件委託
簡單來講:event.target表明當前觸發事件的元素,能夠經過當前元素對象的一系列屬性來判斷是否是咱們想要的元素
this和event.target的區別:
js中事件是會冒泡的,因此this是能夠變化的,但event.target不會變化,它永遠是直接接受事件的目標DOM元素;
.this和event.target都是dom對象
若是要使用jquey中的方法能夠將他們轉換爲jquery對象。好比this和$(this)的使用、event.target和$(event.target)的使用.
triggerHandler不會觸發瀏覽器的默認行爲,.triggerHandler( "submit" )將不會調用表單上的.submit()
.trigger() 會影響全部與 jQuery 對象相匹配的元素,而 .triggerHandler() 僅影響第一個匹配到的元素
使用 .triggerHandler() 觸發的事件,並不會在 DOM 樹中向上冒泡。 若是它們不是由目標元素直接觸發的,那麼它就不會進行任何處理
與普通的方法返回 jQuery 對象(這樣就可以使用鏈式用法)相反,.triggerHandler() 返回最後一個處理的事件的返回值。若是沒有觸發任何事件,會返回 undefined
//給input綁定一個聚焦事件 $("input").on("focus",function(event,title) { $(this).val(title) }); $("#accident").on("click",function() { alert("trigger觸發的事件會在 DOM 樹中向上冒泡"); }); //trigger觸發focus $("button:first").click(function() { $("a").trigger("click"); $("input").trigger("focus"); }); //triggerHandler觸發focus $("button:last").click(function() { $("a").triggerHandler("click"); $("input").triggerHandler("focus","沒有觸發默認聚焦事件"); });