使用javascript觸發(fā)事件(續(xù)),使用jquery.simulate plugin
上篇不完整的介紹了一事件模擬機制(誘發(fā)是simulate的翻譯,在Professional JavaScript for Web Developers這本書的中文版中翻譯為模擬),后來找到了一個jquery.simulate插件,對事件的模擬做了一個精簡的封裝。先看看怎么使用:
<input type="text" id="txt1" name="" value="abc" />
<input type="button" id="btnSimulate" name="" value="Simulate" />
<input type="button" id="btnSimulate" name="" value="Simulate" />
javascript:
$(function(){
$("#txt1").keyup(function(e){
alert(e.keyCode);
});
$("#btnSimulate").click(function(){
$("#txt1").simulate("keyup",{
keyCode:68
});
});
});
用btnSimulate的點擊事件能模擬txt1的keyup事件,uHHHm,還不錯。$("#txt1").keyup(function(e){
alert(e.keyCode);
});
$("#btnSimulate").click(function(){
$("#txt1").simulate("keyup",{
keyCode:68
});
});
});
第一個參數(shù)是事件類型
第二個參數(shù)是事件參數(shù):默認(rèn)為
{
bubbles: true, cancelable: (type != "mousemove"), view: window, detail: 0,
screenX: 0, screenY: 0, clientX: 0, clientY: 0,
ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
button: 0, relatedTarget: undefined
}
與標(biāo)準(zhǔn)的DOM Event事件屬性一致。bubbles: true, cancelable: (type != "mousemove"), view: window, detail: 0,
screenX: 0, screenY: 0, clientX: 0, clientY: 0,
ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,
button: 0, relatedTarget: undefined
}
demo下載
posted on 2010-12-01 11:23 衡鋒 閱讀(537) 評論(0) 編輯 收藏 所屬分類: Web開發(fā)