标签:
next
和 nextAll
方法itcast.extend({
nextSibling: function(dom) {
var nextNode = dom;
while(nextNode = nextNode.nextSibling) {
if(nextNode.nodeType === 1) {
return nextNode;
}
}
}
});
itcast.fn.extend({
next: function() {
var arr = [];
itcast.each(this, function() {
arr.push( itcast.nextSibling(this) );
});
return itcast(arr);
}
});
itcast.extend({
nextAll: function(dom) {
var newDom, arr = [];
while(newDOM = dom.nextSibling) {
if(newDom.nodeType === 1) {
arr.push(newDom);
}
}
return arr;
}
});
itcast.fn.extend({
nextAll: function() {
var arr = [];
itcast.each(this, function(i, v) {
itcast.push.apply(arr, itcast.nextAll(v));
});
return itcast(arr);
}
});
itcast.extend({
select: select
});
// 使用 Sizzle 替换 select
itcast.select = Sizzle;
onclick / onmouseenter / onmouseleave / onmouseover / onmouseout
onkeydown / onkeyup / onfocus / onblur
on...
dv.onclick = function() {
};
<button onclick="alert(‘划船不用桨‘);"></button>
<button onclick="fn();"></button>
<script>
function fn() {
alert("划船不用桨");
}
</script>
// 参数一:事件名
// 参数二:事件处理程序
// 参数三:false表示冒泡,true表示捕获
dv.addEventListener("click", function() {}, false);
// 参数一:事件名
// 参数二:事件处理程序
dv.attachEvent("onclick", function() {});
itcast.fn.extend({
click: function(callback) {
this.each(function() {
this.onclick = callback;
});
}
});
itcast.fn.extend({
click: function(callback) {
this.each(function() {
this.addEventListener("click", callback);
});
return this;
}
});
itcast.fn.extend({
on: function(type, callback) {
this.each(function() {
if(this.addEventListener) {
this.addEventListener(type, callback);
} else {
this.attachEvent("on" + type, callback);
}
});
return this;
},
off: function() {
this.each(function() {
if(this.removeEventListener) {
this.removeEventListener(type, callback);
} else {
this.detachEvent("on" + type, callback);
}
});
return this;
}
});
itcast.fn.extend({
mouseenter: function(type, callback) {
return this.on("mouseenter", callback);
}
});
itcast.each(("click,mouseenter,mouseleave,keydown,keyup").split(","), function(i, v) {
itcast.fn[v] = function(callback) {
return this.on(v, callback);
};
});
1 普通事件绑定方式(如 onclick=..):
W3C标准:使用事件处理程序参数(event)IE9+ / Chrome / FF
IE(8及以下版本)中 :使用 window.event
2 addEventListener 和 attachEvent 都实现了事件参数event
说明:即使是都实现了事件参数event,但是对象中的一些属性值还是存在差异,
例如:which
IE:
button 左1 中4 右2
which 不存在
Chrome:
button 左0 中1 右2
which 左1 中2 右3
attachEvent的兼容性:
支持: IE6 - IE10
不支持: IE11+(支持 addEventListener)
e = e || window.event;
itcast("div").click(function(e) {
e = e || window.event;
// 获取事件类型
alert(e.type);
});
itcast("div").click(function(e) {
alert(e.which);
});
$("div").hover(function() {
$(this).css("background", "#abc");
}, function() {
$(this).css("background", "#fff");
});
itcast.fn.extend({
hover: function(fn1, fn2) {
return this.mouseenter(fn1).mouseleave(fn2);
}
});
if(itcast.isFunction(selector)) {
// window.onload = selector;
window.addEventListener("load", selector);
}
var eventList = [];
window.onload = function() {
for(var i = 0; i < eventList.length; i++) {
eventList[i]();
}
};
if(itcast.isFunction(selector)) {
eventList.push(selector);
}
if(itcast.isFunction(selector)) {
var oldFunc = window.onload;
if(typeof oldFunc === "function") {
window.onload = function() {
selector();
oldFunc();
};
} else {
window.onload = selector;
}
}
var fn = function() {
alert(123);
};
dv.addEventListener("click", fn);
dv.removeEventListener("click", fn);
标签:
原文地址:http://www.cnblogs.com/lsy0403/p/5912198.html