簡單實現jQuery

2021-08-21 19:11:39 字數 990 閱讀 4176

jquery的本質是函式,下面來簡單實現一下jquery中的addclass和text

window.jquery = function

() if(typeof nodeorselector === 'string') else

if(nodeorselector instanceof node)

} else

if(nodeorselector instanceof nodelist)

nodes.length = nodeorselector.length

}nodes.addclass = function

(classes) }}

nodes.text = function

(text)

return texts

}else }}

return nodes

}window.$ = jquery

呼叫:

$(

'div').addclass('a').text('hello world')

簡單來說,jquery是輸入引數為字串(css選擇器),node物件或nodelist物件,輸出乙個可以操縱輸入引數對應的dom節點物件的函式

上述**涉及到的主要知識點有:

1.閉包

如nodes變數和addclass函式就構成乙個閉包,可以通過addclass函式訪問到區域性變數nodes

2.===

完全相等,建議用來替代所有的『==』

3.typeof 和 instanceof

typeof 用來判斷基本資料型別,instanceof用來判斷複雜資料型別(物件)

4.獲取自身物件的可列舉屬性

for( i in obj)

}

jquery實現簡單分頁

這裡我的分頁思路是先建立乙個拼接行函式,然後根據每頁的行數,將陣列再放到分頁函式裡進行分頁。新增行函式 index 預設頁碼 num 一頁的行數 一頁的資料數 list 要拼成tr的資料list function init index,num,list else 分頁函式 function page...

jquery實現簡單輪播

先上簡單的html 在這個html的目錄下有三個同級的資料夾,img中放,css和js分別存放這個網頁的css檔案和js檔案,這裡用到了jquery,記得引入順序,jquery一定要放在其他js前面。html 很簡單,不做過多解釋。init2.css layout banner banner img...

jQuery實現簡單的動畫

1.淡入淡出效果 fadein 淡入隱藏的元素 selector fadein speed,callback 引數 speed 效果時長。可取的值 slow fast 毫秒數 callback 過渡完成後,所執行的方法名稱 div fadein 3000 fadeout 淡出可見的元素 fadeto...