< !DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” ”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Untitled Page</title>
</head>
<body>
<div id=”d”>divvv</div>
<div id=”fsd”>fdsf</div>
</body>
</html>
<script type=”text/javascript”>
//实现自己的MyQuery框架
var MyQuery = function(selector){
if ( window == this ) return new MyQuery(selector);
//这里只实现dom类型的简单查找,嘿嘿
var doms = document.getElementsByTagName(selector);
var arr = [];
for(var i=0; i<doms .length; i++){
arr.push(doms.item(i));
}
return this.setArray(arr);
}
MyQuery.prototype.setArray = function( arr ) {
this.length = 0;
[].push.apply( this, arr );
return this;
}
MyQuery.fn = MyQuery.prototype;
var $ = MyQuery;
//插件扩展1)each
MyQuery.fn.each = function(method){
for(var i=0,l=this.length; i<l; i++){
method.call(this[i],i);
}
}
//插件扩展2)show
MyQuery.fn.show = function(){
this.each(function(i){
alert(i+“:“+this.id+“:“+this.innerHTML);
});
}
//debugger
$(“div“).show();
</script></doms></script>
开发自己的js框架
最新推荐文章于 2024-07-08 08:20:53 发布