欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

模仿jQuery each函數(shù)的鏈?zhǔn)秸{(diào)用

 更新時(shí)間:2009年07月22日 00:16:29   作者:  
模仿jQuery each函數(shù)的鏈?zhǔn)秸{(diào)用實(shí)現(xiàn)代碼。
復(fù)制代碼 代碼如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Each Document</title>
<script type="text/javascript">
(function(){ //創(chuàng)建一個(gè)自執(zhí)行函數(shù)
function _$(el){ //聲明一個(gè)類
this.elements = []; //用于保存DOM元素的引用
for (var i = 0; i < el.length; i++) {
var element = el[i];
if (typeof element == string ) {
element = document.getElementById(element);
}
this.elements.push(element);
}
}

_$.prototype = {
each: function(fn){
for (var i = 0; i < this.elements.length; i++) {
fn.call(this, this.elements[i]); //第二個(gè)參數(shù)是fn函數(shù)的參數(shù)
}
},
setStyle: function(prop, val){
var that = this;
this.each(function(el){ //注意參數(shù)的設(shè)置
el.style[prop] = val;
});
return this;
}
}

window.$ = function(){
return new _$(arguments);
};
})();

window.onload = function(){
$("p", "p2").setStyle("color", "red");
}
</script>
</head>
<body>
<p id="p">
Hello world
</p>
<p id="p2">
Welcome.
</p>
</body>
</html>

相關(guān)文章

最新評(píng)論