jQuery初體驗(yàn)

1. 封裝一個函數(shù)

<body>
  <ul>
    <li id="item1">選項(xiàng)1</li>
    <li id="item2">選項(xiàng)2</li>
    <li id="item3">選項(xiàng)3</li>
    <li id="item4">選項(xiàng)4</li>
    <li id="item5">選項(xiàng)5</li>
  </ul>
</body>
-----------------------------
var allChildren = item3.parentNote.children
var array = {
    length: 0
}
for (var i = 0; i < allChildren.length; i++) {
    if (allChildren[i] !== item3) {
        array[array.length] = allChildren[i]
        array.length += 1
    }
}
// 封裝函數(shù)唤蔗,獲取自己的兄弟姐妹
function getSiblings(node) { /*API*/
    var allChildren = node.parentNote.children
    var array = {
        length: 0
    }
    for (var i = 0; i < allChildren.length; i++) {
        if (allChildren[i] !== node) {
            array[array.length] = allChildren[i]
            array.length += 1
        }
    }
    return array
}
getSibings(item3)

1.1 再封裝一個

var classes = {
    'a': true,
    'b': false,
    'c': true
}
for (let key in classes) {
    var value = classes[key]
    if (value) {
        items3.classList.add(key)
    } else {
        items3.classList.remove(key)
    }
}
// 封裝函數(shù) 添加或者移除class
function addClass(node, classes) {
    for (let key in classes) {
        var value = classes[key]
        -- 優(yōu)化前 ---------------------
        if (value) {
            node.classList.add(key)
        } else {
            node.classList.remove(key)
        }
        -- 優(yōu)化后 ---------------------
        var methodName = value ? 'add' : 'remove'
        node.classList[methodName](key)
    }
}
addClass(item3, {a: true, b: false, c: true})

代碼優(yōu)化守則:

如果出現(xiàn)類似的代碼掷空,就存在優(yōu)化的可能

2. 命名空間

2.1

window.yydom = {}
yydom.getSiblings = getSiblings
yydom.addClass = addClass

yydom.getSiblings(item3)
yydom.addClass(item3, {a: true, b: false, c:true})

2.2

window.yydom = {}
yydom.getSiblings = function (node) { /*API*/
    var allChildren = node.parentNote.children
    var array = {
        length: 0
    }
    for (var i = 0; i < allChildren.length; i++) {
        if (allChildren[i] !== node) {
            array[array.length] = allChildren[i]
            array.length += 1
        }
    }
    return array
}

yydom.addClass = function (node, classes) {
    for (let key in classes) {
        var value = classes[key]
        -- 優(yōu)化前 ---------------------
        if (value) {
            node.classList.add(key)
        } else {
            node.classList.remove(key)
        }
        -- 優(yōu)化后 ---------------------
        var methodName = value ? 'add' : 'remove'
        node.classList[methodName](key)
    }
}

yydom.getSiblings(item3)
yydom.addClass(item3, {a: true, b: false, c:true})

2.3

Node.prototype.getSiblings = function () {      // Node.prototype中增加了getSiblings方法
    var allChildren = this.parentNote.children
    var array = {
        length: 0
    }
    for (var i = 0; i < allChildren.length; i++) {
        if (allChildren[i] !== this) {
            array[array.length] = allChildren[i]
            array.length += 1
        }
    }
    return array
}
items3.getSibings()     // this就是.前面的
items3.getSiblings.call(item3)  // 用call調(diào)用


Node.prototype.addClass = function (classes) { // this不用傳
    for (let key in classes) {
        var value = classes[key]
        var methodName = value ? 'add' : 'remove'
        this.classList[methodName](key)
    }
}
items3.addClass({a: true, b: false, c: true})
items3.addClass.call(items3, {a: true, b: false, c: true})

不用命名空間的缺點(diǎn):

別人不知道你的庫叫什么

會不知不覺把全局變量給覆蓋了

2.4

window.Node2 = function (node) {
    return {
        getSiblings: function (node) {
            var allChildren = node.parentNote.children
            var array = {
                length: 0
            }
            for (var i = 0; i < allChildren.length; i++) {
                if (allChildren[i] !== node) {
                    array[array.length] = allChildren[i]
                    array.length += 1
                }
            }
            return array
        },
        addClass: function (classes) {
            for (let key in classes) {
                var value = classes[key]
                var methodName = value ? 'add' : 'remove'
                node.classList[methodName](key)
            }
        }
    }
}
var node2 = Node2(item3)
node2.getSiblings()
node2.addClass(['a', 'b', 'c'])

2.5

window.jQuery = function (node) {
    return {  // 返回的是對象 key: value
        getSiblings: function (node) {
            var allChildren = node.parentNote.children
            var array = {
                length: 0
            }
            for (var i = 0; i < allChildren.length; i++) {
                if (allChildren[i] !== node) {
                    array[array.length] = allChildren[i]
                    array.length += 1
                }
            }
            return array
        },
        addClass: function (classes) {
            for (let key in classes) {
                var value = classes[key]
                var methodName = value ? 'add' : 'remove'
                node.classList[methodName](key)
            }
        }
    }
}
var node2 = jQuery(item3)
node2.getSiblings()
node2.addClass(['a', 'b', 'c'])

2.6

window.jQuery = function (nodeOrSelector) {
    let node
    // 判斷nodeOrSelector類型
    if (typeof nodeOrSelector === 'string') {
        node = document.querySelector(nodeOrSelector)
    } else {
        node = nodeOrSelector
    }
    return {  // 返回的是對象 key: value
        getSiblings: function (node) {
            var allChildren = node.parentNote.children
            var array = {
                length: 0
            }
            for (var i = 0; i < allChildren.length; i++) {
                if (allChildren[i] !== node) {
                    array[array.length] = allChildren[i]
                    array.length += 1
                }
            }
            return array
        },
        addClass: function (classes) {
            for (let key in classes) {
                var value = classes[key]
                var methodName = value ? 'add' : 'remove'
                node.classList[methodName](key)
            }
        }
    }
}
var node2 = jQuery("#item3")
node2.getSiblings()
node2.addClass(['a', 'b', 'c'])

2.7

jQuery初體驗(yàn)

window.jQuery = function (nodeOrSelector) {
    let nodes = {}
    // 判斷nodeOrSelector類型
    if (typeof nodeOrSelector === 'string') {
        let temp = document.querySelectorAll(nodeOrSelector)
        for (let i = 0; i < temp.length; i++) {
            nodes[i] = temp[i]
        }
        nodes.length = temp.length
    } else {
        nodes = {
            0: nodeOrSelector,
            length: 1
        }
    }
   
    nodes.addClass = function (classes) {
            for (let key in classes) {
                var value = classes[key]
                var methodName = value ? 'add' : 'remove'
                nodes.classList[methodName](key)
            }
        }
    }
    nodes.text = function () {
        if (text === undefined) { // 如果參數(shù)不存在润绵,則獲取文本
            var texts = []
            for (let i = 0; i < nodes.length: i++) {
                texts.push(nodes[i].textContent)
            }
            return texts
        } else {    // 如果參數(shù)存在汛蝙,則傳入文本
            for (let i = 0; i < nodes.length: i++) {
                nodes[i].textContext = text
            }
        }
    }
    return nodes
}
var node2 = jQuery('ul' > 'li')
node2.addClass(['blue'])
node2.text()

3. jQuery初探

var node2 = jQuery('ul > li')
node2.addClass('red')   // 添加類名
node2.removeClass('red')    // 刪除類名
node2.togoleClass('red)     // 切換 
x.onclick = addClass(function (index, currentClass)) {  // 兩個參數(shù)必填
    return 'c-' + index   // 添加類名 c-0 c-1
}

var classes = ['red', 'yellow', 'blue', 'green', 'pink']
x.onclick = addClass(function (index, currentClass)) {  // 兩個參數(shù)必填
    return classes[index]   // 添加類名 c-0 c-1
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市逼肯,隨后出現(xiàn)的幾起案子膜钓,更是在濱河造成了極大的恐慌,老刑警劉巖醋拧,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件慷嗜,死亡現(xiàn)場離奇詭異,居然都是意外死亡丹壕,警方通過查閱死者的電腦和手機(jī)庆械,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來雀费,“玉大人干奢,你說我怎么就攤上這事≌蛋溃” “怎么了忿峻?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長辕羽。 經(jīng)常有香客問我逛尚,道長,這世上最難降的妖魔是什么刁愿? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任绰寞,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘滤钱。我一直安慰自己觉壶,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布件缸。 她就那樣靜靜地躺著铜靶,像睡著了一般。 火紅的嫁衣襯著肌膚如雪他炊。 梳的紋絲不亂的頭發(fā)上争剿,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天,我揣著相機(jī)與錄音痊末,去河邊找鬼蚕苇。 笑死,一個胖子當(dāng)著我的面吹牛凿叠,可吹牛的內(nèi)容都是我干的涩笤。 我是一名探鬼主播,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼幔嫂,長吁一口氣:“原來是場噩夢啊……” “哼辆它!你這毒婦竟也來了誊薄?” 一聲冷哼從身側(cè)響起履恩,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎呢蔫,沒想到半個月后切心,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡片吊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年绽昏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片俏脊。...
    茶點(diǎn)故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡全谤,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出爷贫,到底是詐尸還是另有隱情认然,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布漫萄,位于F島的核電站卷员,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏腾务。R本人自食惡果不足惜毕骡,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧未巫,春花似錦窿撬、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至狭姨,卻和暖如春宰啦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背饼拍。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工赡模, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人师抄。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓漓柑,卻偏偏與公主長得像,于是被迫代替她去往敵國和親叨吮。 傳聞我的和親對象是個殘疾皇子辆布,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容