读jQuery源码释疑笔记

前端开发 作者: 2024-08-26 10:25:01
本释疑笔记是针对自己在看源码的过程中遇到的一些问题的解答,对大众可能不具有参考性,不过可以看看有没有你也不懂得地方,相互学习,相互进步。 1、each的用法 之前对each的用法一直迷迷糊糊,这次终于
each: function( obj,callback,args ) {
        var value,i = 0,length = obj.length,isArray = isArraylike( obj );

        if ( args ) {
             ( isArray ) {
                for ( ; i < length; i++ ) {
                    value = callback.apply( obj[ i ],args );

                    if ( value === false ) {
                        break;
                    }
                }
            } else {
                for ( i in obj ) {
                    value =;
                    }
                }
            }

        // A special,fast,case for the most common use of each
        }  {
             callback.call( obj[ i ],i,obj[ i ] );

                    ;
                    }
                }
            }
        }

        return obj;
    },
View Code
var t={name:"t",age:"t1"};
    var tt=[1,2,3];

    $.each(t,(j){
    console.log(j);  1;   1    每次迭代输出一个值
    },tt)

    $.each(tt,1)">1;   1
    },t)undefinded ;undefined ;undefined   每次迭代输出一个值

    $.each(t,1)">(j){
    console.log(j); name; age
    })

    $.each(t,1)">(i,j){
    console.log(i+" "+j);name t; age t1
    })

    $.each(tt,1)">0 1; 1 2; 2 3;
    })
var name="t";
var getByName = typeof name === "string";  name必须是字符串
console.log(getByName);  true
console.log(name);  t

//类似的还出现在access函数中。
bulk = key == null;

var str = "Visit W3School"; 
var patt = new RegExp("W3School","g");
 result;

while ((result = patt.exec(str)) != null)  {
  document.write(result);
  document.write("<br />");
  document.write(patt.lastIndex);
 }

输出:
W3School
14
  • ownerDocument是Node对象的一个属性,返回的是某个元素的根节点文档对象:即document对象
  • documentElement是Document对象的属性,返回的是文档根节点
  • 对于HTML文档来说,documentElement是<html>标签对应的Element对象,ownerDocument是document对象
<div id="one"></div>
    <div></div>
    <script >
    var dom=document.getElementById("one");
    console.log($("div").ownerDocument);undefined
    console.log(document.ownerDocument);null
    console.log(document.documentElement.ownerDocument);document对象
    console.log(document.body.ownerDocument);document对象
    console.log(dom.ownerDocument);document对象
    console.log(dom.nodeType);1
    console.log(window.document.nodeType);9
    console.log(window.document);document对象
    console.log(document.body.ownerDocument.defaultView);window对象
    console.log(dom.ownerDocument.defaultView);window对象
    console.log(dom.ownerDocument.documentElement);<html><head><body>....是标签
    </script>
str = "hello,<b>my name is</b> jQuery.",html = $.parseHTML( str ),
strundefined = typeof undefined,console.log(strundefined+"  "+$.type(strundefined));undeined  string
  • 父亲father与儿子child1属于父子关系,也算是祖先与后代关系(>表达)
  • 哥哥child1与弟弟child2属于临近兄弟关系(+表达)
  • 哥哥child1与弟弟child2,弟弟child3都属于普通兄弟关系(~表达)
  $("#one").removeClass("");
    console.log( $('#one').attr('class') );1 huanshen  huansky
    $("#one").removeClass();
    console.log( $('#one').attr('class') );//

    var t=null ? 1 : 0;
    console.log(t);0

    var t=" " ? 1 : 01

    var t="" ? 1 : 0 sum(){
        console.log(arguments.length);1
    };
    sum("");
$('#element).delegate('a','click',function() { alert("!!!") });
$('a').live('click',1)"> one,只触发一次
one: function( types,selector,data,fn ) {
    return this.on( types,fn,1 );
},
$('.ul').on('click','a',function(e){  alert('click event');});

.bind()方法用于直接附加一个事件处理程序到元素上。
  • 为DOM中的很多元素绑定相同事件;
  • 为DOM中尚不存在的元素绑定事件;
  • 并非所有的事件都能冒泡,如load,change,submit,focus,blur
  • 加大管理复杂。
  • 不好模拟用户触发事件
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68919.html
读jQuery源码释疑笔记