(function ($) {
//jquery object擴展
$.extend($.fn, {
BindCheck: function (id, type, callback) {
///<summary>檢查有沒有重覆的id,沒有才Bind,如果沒有給id,就判斷有沒有重覆的type</summary>
if (arguments.length == 2) {
callback = type;
type = id;
if (!(this.data("events") && this.data("events")[type])) {
this.bind(type, callback);
}
} else {
var bindCheck = $.data(this,"BindCheck") || $.data(this,"BindCheck", {})
if (!bindCheck[id]) {
this.bind(type, callback);
bindCheck[id] = true;
}
}
}
});
})(jQuery)
$(function(){
$("#test1").BindCheck("click",function(){ alert(1)});
$("#test1").BindCheck("click",function(){ alert(2)});
$("#test1").BindCheck("click",function(){ alert(3)});
$("#test2").BindCheck("test2-1","click",function(){ alert(1)});
$("#test2").BindCheck("test2-1","click",function(){ alert(2)});
$("#test2").BindCheck("test2-2","click",function(){ alert(3)});
$("#test2").BindCheck("test2-2","click",function(){ alert(4)});
})
<input id="test1" type='button' value="test 1"/>
<input id="test2" type='button' value="test 2"/>