美文网首页
jQuery初体验

jQuery初体验

作者: 酒当茶 | 来源:发表于2019-02-22 20:17 被阅读0次

1. 封装一个函数

<body>
  <ul>
    <li id="item1">选项1</li>
    <li id="item2">选项2</li>
    <li id="item3">选项3</li>
    <li id="item4">选项4</li>
    <li id="item5">选项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
    }
}
// 封装函数,获取自己的兄弟姐妹
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)
    }
}
// 封装函数 添加或者移除class
function addClass(node, classes) {
    for (let key in classes) {
        var value = classes[key]
        -- 优化前 ---------------------
        if (value) {
            node.classList.add(key)
        } else {
            node.classList.remove(key)
        }
        -- 优化后 ---------------------
        var methodName = value ? 'add' : 'remove'
        node.classList[methodName](key)
    }
}
addClass(item3, {a: true, b: false, c: true})

代码优化守则:

如果出现类似的代码,就存在优化的可能

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]
        -- 优化前 ---------------------
        if (value) {
            node.classList.add(key)
        } else {
            node.classList.remove(key)
        }
        -- 优化后 ---------------------
        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调用


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})

不用命名空间的缺点:

别人不知道你的库叫什么

会不知不觉把全局变量给覆盖了

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初体验

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) { // 如果参数不存在,则获取文本
            var texts = []
            for (let i = 0; i < nodes.length: i++) {
                texts.push(nodes[i].textContent)
            }
            return texts
        } else {    // 如果参数存在,则传入文本
            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)) {  // 两个参数必填
    return 'c-' + index   // 添加类名 c-0 c-1
}

var classes = ['red', 'yellow', 'blue', 'green', 'pink']
x.onclick = addClass(function (index, currentClass)) {  // 两个参数必填
    return classes[index]   // 添加类名 c-0 c-1
}

相关文章

  • JQUERY UI使用初体验

    JQUERY UI使用初体验:JQUERY UI网址:https://jqueryui.com/ 相关文件下载地址...

  • jQuery 初体验

    什么是JQ? 一个优秀的JS库 写越少的代码,做越多的事情 JQ的好处? 简化JS的复杂操作 不再需要关心兼容性 ...

  • jQuery初体验

    作业题 HTML代码 CSS代码 JS代码 jQuery 接受旧节点,返回新的对象,新API调用的还是DOM得AP...

  • jQuery初体验

    1. 封装一个函数 1.1 再封装一个 代码优化守则:如果出现类似的代码,就存在优化的可能 2. 命名空间 2.1...

  • Vue 初体验

    Vue 初体验 原生 JS 写项目的问题 语法冗长,复杂,操作页面麻烦,效率低 JQuery 开发的问题 提供了简...

  • 2019-11-28

    重回简书-----我最初认识的文字软件 从今天我将介绍一下我学习进度与学习心得。 今天学习到了jquery的初体验...

  • yii初体验(7-15)

    yii初体验(7)视图 yii初体验(8)模块 yii初体验(9) 小部件widgets yii初体验(10) 前...

  • 动画篇-layout动画初体验

    动画篇-layout动画初体验 动画篇-layout动画初体验

  • 劳动主题画报

    一、实践画报(含体验日记) _____初体验(如:记者初体验、医生初体验等) 说明: 1.利用假期亲身体验一项工作...

  • 简约不简单

    初体验

网友评论

      本文标题:jQuery初体验

      本文链接:https://www.haomeiwen.com/subject/ipryyqtx.html