




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第你值得了解的JavaScript“繼承之jquery”使用方法(代碼詳解)假如不用,學(xué)習(xí)下還是可以的
本文粗燥的實(shí)現(xiàn)jquery的ready、each、bind、``$.fn.extend、$.extend
初始化$
(function(win){
var_$=function(selector,context){
*通常咱們定義一個(gè)函數(shù)varFun=function(){}
*然后定義一個(gè)Ftotype.init=function(){}
*那么咱們調(diào)用init的時(shí)候得先要實(shí)例化對象varf=newFun()
*然后f.init()
*這里就省去了var$=new$()
returnnew_$.prototype.Init(selector,context);
_$.prototype={
//初始化$
Init:function(selector,context){
this.elements=[];
*傳入的類型是function就執(zhí)行ready事件,如果是document就將document對象插入到this.elements
*主要就是判斷$(document).ready和$(function(){})這兩種的ready事件的寫法
if(typeofselector===function){
this.elements.push(document);
this.ready(selector);
}else{
varcontext=context||document;
varisDocument=(ele)=
Ototype.toString.call(ele)==[objectHTMLDocument]||
[objectDocument]
if(isDocument(selector)){
this.elements.push(selector);
}else{
*如果是字符串的話就查詢該節(jié)點(diǎn)$(.class)|$(#id)
if(context.querySelectorAll){
vararr=context.querySelectorAll(selector);
for(vari=0;iarr.length;i++){
this.elements.push(arr[i]);
//實(shí)現(xiàn)each
each:function(callback){},
//實(shí)現(xiàn)ready
ready:function(callback){},
//實(shí)現(xiàn)bind
bind:function(type,callback){},
*讓兩個(gè)作用域不一樣的對象共享一個(gè)方法,讓他們的原型指向一致,即Itotype=_$.prototype
*那么原型一致之后就可以共享this.elements屬性了。
_$.prototype.Itotype=_$.prototype;
window.$=_$;
})(window||global);
ready
//實(shí)現(xiàn)ready
ready:function(callback){
varisDocument=(ele)=Ototype.toString.call(ele)==[objectHTMLDocument]||[objectDocument]
//如果已經(jīng)取得了節(jié)點(diǎn)
if(isDocument(this.elements[0])){
if(document.addEventListener){//判斷火狐、谷歌
*DOM樹構(gòu)建完成的時(shí)候就會(huì)執(zhí)行DOMContentLoaded
*頁面上所有的DOM,樣式表,腳本,圖片,flash都已經(jīng)加載完成了,才會(huì)觸發(fā)window.onload
*這也就是$(document).ready()比window.onload執(zhí)行早的原因
*arguments.callee博客里面有一篇文章[js-遞歸]里面專門講到了,這里不再解釋了
document.addEventListener(DOMContentLoaded,function(){
document.removeEventListener(DOMContentLoaded,arguments.callee,false)
callback()
},false)
}elseif(document.attachEvent){//判斷IE
document.attachEvent(onreadystatechange,function(){
if(document.readyState==complete){
document.detachEvent(onreadystatechange,arguments.callee);
callback()
}elseif(document.lastChild==document.body){//body已經(jīng)加載完了,就直接回調(diào)了
callback()
},
each
//實(shí)現(xiàn)each
each:function(callback){
if(this.elements.length0){
for(vari=0;ithis.elements.length;i++){
callback.call(this,this.elements[i],i);
},
bind
//實(shí)現(xiàn)bind
bind:function(type,callback){
if(document.addEventListener){//判斷火狐、谷歌
this.each(function(item,i){
item.addEventListener(type,callback,false)
}elseif(document.attachEvent){//判斷IE
this.each(function(item,i){
item.attachEvent(on+type,callback)
}else{
this.each(function(item,i){//其他瀏覽器egg:item.onclick=function(){}
item[on+type]=callback
}
$.fn.extend/$.extend
$.fn.extend是為查詢的節(jié)點(diǎn)對象擴(kuò)展方法,是基于$的原型擴(kuò)展的方法
$.extend是擴(kuò)展常規(guī)方法,是$的靜態(tài)方法
官方給出解釋:
jQuery.extend():Mergethecontentsoftwoormoreobjectstogetherintothefirstobject.(把兩個(gè)或者更多的對象合并到第一個(gè)當(dāng)中)
jQuery.fn.extend():MergethecontentsofanobjectontothejQueryprototypetoprovidenewjQueryinstancemethods.(把對象掛載到j(luò)Query的prototype屬性,來擴(kuò)展一個(gè)新的jQuery實(shí)例方法)
$.fn.extend方法的初衷是我們擴(kuò)展之后可以用$().newMetod()這樣訪問,實(shí)際上就是給$原型加一個(gè)extend方法。這中間的fn其實(shí)類似于命名空間的作用,沒什么實(shí)際的意義。為的是和$.extend作區(qū)分
$.fn.extend
;(function(win){
_$.prototype.Itotype=_$.prototype;
_$.fn=_$.prototype;//把對象掛載到j(luò)Query的prototype屬性
varisObj=(o)=Ototype.toString().call(o)===[objectObject]
$.fn.extend=function(obj){
if(isObj(obj)){
for(variinobj){
this[i]=obj//注意這里的this指向是$.prototype
}
$.extend
varisObj=(o)=Ototype.toString().call(o)===[objectObject]
_$.extend=function(obj){
if(isObj(obj)){
for(variinobj){
this[i]=obj[i];//注意這里的this指向是$
}
這倆看上去一模一樣啊,沒啥區(qū)別,注釋里面已經(jīng)說了,this指向不同。咱們來看個(gè)例子:
!DOCTYPEhtml
html
head
titlejQuery.extend()與jQuery.fn.extend()區(qū)別/title
metacharset=utf-8/
scripttype=text/javascriptsrc=jquery.js/script
!--開始擴(kuò)展--
scripttype=text/javascript
(function($){
$.extend({
sayHello:function(){
console.log(Hello
$.fn.extend({
sayHello:function(){
console.log(Hello
})(jQuery);
/script
!--調(diào)用--
scripttype=text/javascript
$(document).ready(function(){
//$.extend擴(kuò)展調(diào)用
$.sayHello();
//$.fn.extend擴(kuò)展調(diào)用
$(#test).sayHello();
/script
/head
body
divid=test/div
/body
/html
這樣以來就看的很明白了。jQuery.extend(object);為擴(kuò)展jQuery類本身,為自身添加新的方法。$.xxx()
jQuery.fn.extend(object);給jQuery對象添加方法$(#test).xxx()
$.extend常見用法
//在jquery全局對象中擴(kuò)展一個(gè)net命名空間。
$.extend({net:{}});
//方法擴(kuò)展到之前擴(kuò)展的Jquery的net命名空間中去。
$.extend($.net,{
sayHello:function(){
console.log(Hello
//extend方法還有一個(gè)重載原型
//extend(boolean,dest,src1,src2,src3...),第一個(gè)參數(shù)boolean代表是否進(jìn)行深度拷貝
vara={protocol:http,hash:{a:1,b:2}};
varb={host:,hash:{b:1,c:2}};
varresult=$.extend(true,{},a,b);
console.log(result);//{protocol:http,host:,hash:{a:1,b:1,c:2}}
varresult=$.extend(false,{},a,b);
console.log(result);//{protocol:http,host:,hash:{b:1,c:2}}
完整代碼
(function(win){
var_$=function(selector,context){
*通常咱們定義一個(gè)函數(shù)varFun=function(){}
*然后定義一個(gè)Ftotype.init=function(){}
*那么咱們調(diào)用init的時(shí)候得先要實(shí)例化對象varf=newFun()
*然后f.init()
*這里就省去了var$=new$()
returnnew_$.prototype.Init(selector,context);
_$.prototype={
//初始化$
Init:function(selector,context){
this.elements=[];
*傳入的類型是function就執(zhí)行ready事件,如果是document就將document對象插入到this.elements
*主要就是判斷$(document).ready和$(function(){})這兩種的ready事件的寫法
if(typeofselector===function){
this.elements.push(document);
this.ready(selector);
}else{
varcontext=context||document;
varisDocument=(ele)=
Ototype.toString.call(ele)==[objectHTMLDocument]||
[objectDocument]
if(isDocument(selector)){
this.elements.push(selector);
}else{
*如果是字符串的話就查詢該節(jié)點(diǎn)$(.class)|$(#id)
if(context.querySelectorAll){
vararr=context.querySelectorAll(selector);
for(vari=0;iarr.length;i++){
this.elements.push(arr[i]);
//實(shí)現(xiàn)each
each:function(callback){
if(this.elements.length0){
for(vari=0;ithis.elements.length;i++){
callback.call(this,this.elements[i],i);
//實(shí)現(xiàn)ready
ready:function(callback){
varisDocument=(ele)=
Ototype.toString.call(ele)==[objectHTMLDocument]||
[objectDocument]
//如果已經(jīng)取得了節(jié)點(diǎn)
if(isDocument(this.elements[0])){
if(document.addEventListener){
//判斷火狐、谷歌
*DOM樹構(gòu)建完成的時(shí)候就會(huì)執(zhí)行DOMContentLoaded
*頁面上所有的DOM,樣式表,腳本,圖片,flash都已經(jīng)加載完成了,才會(huì)觸發(fā)window.onload
*這也就是$(document).ready()比window.onload執(zhí)行早的原因
*arguments.callee博客里面有一篇文章js-遞歸里面專門講到了,這里不再解釋了
document.addEventListener(
DOMContentLoaded,
function(){
document.removeEventListener(
DOMContentLoaded,
arguments.callee,
false
callback();
false
}elseif(document.attachEvent){
//判斷IE
document.attachEvent(onreadystatechange,function(){
if(document.readyState==complete){
document.detachEvent(onreadystatechange,arguments.callee);
callback();
}elseif(document.lastChild==document.body){
//body已經(jīng)加載完了,就直接回調(diào)了
callback();
//實(shí)現(xiàn)bind
bind:function(type,callback){
if(document.addEventListener){
//判斷火
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 小學(xué)生辯論賽背景課件
- 通信基站設(shè)備采購、安裝及優(yōu)化合同
- 車輛轉(zhuǎn)讓及新能源充電樁安裝與運(yùn)營服務(wù)合同
- 代付水利工程款三方代付協(xié)議
- 電力及鄉(xiāng)村工作政策法規(guī)知識(shí)考試試卷
- 怎樣評教學(xué)課件
- 品牌故事與消費(fèi)者情感路徑構(gòu)建考核試卷
- 粘合劑與密封劑在藝術(shù)品修復(fù)中的應(yīng)用考核試卷
- 氣囊材料中單體對材料抗撕裂強(qiáng)度的貢獻(xiàn)考核試卷
- 儀器精度校準(zhǔn)的實(shí)驗(yàn)室能力評估考核試卷
- 2024年 黃岡市法院系統(tǒng)招聘審判輔助人員考試真題試題含答案
- ktv營銷經(jīng)理管理制度
- 公司消防網(wǎng)格化管理制度
- 護(hù)士職業(yè)溝通技巧課件
- 2025至2030中國氧化鋁纖維行業(yè)供需趨勢及投資風(fēng)險(xiǎn)報(bào)告
- NAMPT調(diào)控NAD代謝影響椎間盤退變的分子機(jī)制與干預(yù)策略研究
- 健康教育大講堂:跌倒
- T/CHES 43-2020水利水電工程白蟻實(shí)時(shí)自動(dòng)化監(jiān)測預(yù)警系統(tǒng)技術(shù)規(guī)范
- 學(xué)習(xí)給復(fù)旦大學(xué)建校120周年賀信心得體會(huì)
- 2025房縣事業(yè)單位筆試真題
- 2025-2030年注塑機(jī)產(chǎn)業(yè)行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報(bào)告
評論
0/150
提交評論