<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>
<title><![CDATA[klvoek]]></title>
<link><![CDATA[http://www.itivy.com/klvoek]]></link>
<description><![CDATA[klvoek]]></description>
<language><![CDATA[zh-cn]]></language>
<copyright><![CDATA[]]></copyright>
<webMaster><![CDATA[]]></webMaster>
<generator><![CDATA[]]></generator>
<Image><![CDATA[]]></Image>
<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/js-undefined-undeclared.html]]></link>
<title><![CDATA[js undefined 和 undeclared]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 16:47:10 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p>在stackoverflow上看到如下一个问题</p>
<p>如果window上的a属性没有定义，如下代码不会引发异常</p>
<p><pre class="brush:js;">if(window.a){
    //some code
}</pre>如果a变量没有定义，如下代码会引发异常</p>
<p><pre class="brush:js;">if(a){
    // some code 
}</pre><p><img src="/Upload/EditorImage/image/klvoek/201202/20120212164058_9441.jpg" title="js-undefined-undeclared" alt="js-undefined-undeclared" border="0" /></p>
这是为什么呢？</p>
<p>后面高手回答：（<span style="font-family:'Trebuchet MS', 'Liberation Sans', 'DejaVu Sans', sans-serif;font-size:22px;line-height:34px;text-align:left;"><a href="http://stackoverflow.com/users/73603/torok-gabor" target="_blank">Török Gábor</a></span>）</p>
<p><p>if(window.a) 是访问了window对象上一个未定义的属性a。if(a) 则是访问了一个未定义的变量。</p>
<p>使用变量时需要先使用var关键字定义这个变量。如果你不定义的话，编译器则会引发一个异常。</p>
<p>对象的属性不需要显示定义就可以使用而不会引发任何异常，但是会得到未定义的值 undefined .</p>
</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-thirteen.html]]></link>
<title><![CDATA[（十三）jQuery的Ajax功能扩展代码]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 16:18:47 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">看了一边Ajax功能模块代码的实现，最后倒叙查看代码。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">最后面httpData 和 param两个函数的实现比较简单。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Get the data out of an XMLHttpRequest.</span><span style="line-height:1.5;color:#008000;"> //</span><span style="line-height:1.5;color:#008000;"> Return parsed XML if content-type header is "xml" and type is "xml" or omitted,</span><span style="line-height:1.5;color:#008000;"> //</span><span style="line-height:1.5;color:#008000;"> otherwise return plain text.</span><span style="line-height:1.5;color:#008000;"> </span>httpData: <span style="line-height:1.5;color:#0000ff;">function</span>(r,type) {
    <span style="line-height:1.5;color:#0000ff;">var</span> ct = r.getResponseHeader("content-type");
    <span style="line-height:1.5;color:#0000ff;">var</span> data = !type &amp;&amp; ct &amp;&amp; ct.indexOf("xml") &gt;= 0;
    data = type == "xml" || data ? r.responseXML : r.responseText;
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If the type is "script", eval it</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( type == "script" ) eval.call( window, data );
    <span style="line-height:1.5;color:#0000ff;">return</span> data;
},<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Serialize an array of form elements or a set of</span><span style="line-height:1.5;color:#008000;"> //</span><span style="line-height:1.5;color:#008000;"> key/values into a query string</span><span style="line-height:1.5;color:#008000;"> </span>param: <span style="line-height:1.5;color:#0000ff;">function</span>(a) {
    <span style="line-height:1.5;color:#0000ff;">var</span> s = [];
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If an array was passed in, assume that it is an array</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> of form elements</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( a.constructor == Array ) {
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Serialize the form elements</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; a.length; i++ )
            s.push( a[i].name + "=" + encodeURIComponent( a[i].value ) );
         
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Otherwise, assume that it's an object of key/value pairs</span><span style="line-height:1.5;color:#008000;"> </span>    } <span style="line-height:1.5;color:#0000ff;">else</span> {
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Serialize the key/values</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> j <span style="line-height:1.5;color:#0000ff;">in</span> a )
            s.push( j + "=" + encodeURIComponent( a[j] ) );
    }
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Return the resulting serialization</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">return</span> s.join("&amp;");
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348050.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">httpData的参数r为response，type是响应内容的格式。当没有指定type的时候则获取响应中指定的类型（仅判断是否是xml，如果不是则把响应内容当作文本处理）。如果指明type类型为script，则使用eval执行。httpData里区分了三种类型 text、xml、script[javascript]</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">param方法是将传入的请求数据构造成请求字符串。这里面用到了数组的push和join方法。encodeURIComponent方法是JavaScript内置方法。原来param方法实现这么简单，以前还一直觉得ajax接口传递对象或者数组很神奇的。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">在向上看的时候发现了httpSuccess、httpIsModified用于判断请求是否成功以及指定的url的内容是否发生修改。简单代码看看呗：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Counter for holding the number of active queries</span><span style="line-height:1.5;color:#008000;"> </span>active: 0,<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Determines if an XMLHttpRequest was successful or not</span><span style="line-height:1.5;color:#008000;"> </span>httpSuccess: <span style="line-height:1.5;color:#0000ff;">function</span>(r) {
    <span style="line-height:1.5;color:#0000ff;">try</span> {
        <span style="line-height:1.5;color:#0000ff;">return</span> !r.status &amp;&amp; location.protocol == "file:" ||
            ( r.status &gt;= 200 &amp;&amp; r.status &lt; 300 ) || r.status == 304 ||
            jQuery.browser.safari &amp;&amp; r.status == undefined;
    } <span style="line-height:1.5;color:#0000ff;">catch</span>(e){}
    <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">false</span>;
},<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Determines if an XMLHttpRequest returns NotModified</span><span style="line-height:1.5;color:#008000;"> </span>httpNotModified: <span style="line-height:1.5;color:#0000ff;">function</span>(xml, url) {
    <span style="line-height:1.5;color:#0000ff;">try</span> {
        <span style="line-height:1.5;color:#0000ff;">var</span> xmlRes = xml.getResponseHeader("Last-Modified");
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Firefox always returns 200. check Last-Modified date</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">return</span> xml.status == 304 || xmlRes == jQuery.lastModified[url] ||
            jQuery.browser.safari &amp;&amp; xml.status == undefined;
    } <span style="line-height:1.5;color:#0000ff;">catch</span>(e){}
    <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">false</span>;
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348050.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">看看httpSuccess判断是否响应成功兼容浏览器的实现。httpNotModified如何判断请求是否返回了未修改的消息。active记录了当前活动态的请求数。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Last-Modified header cache for next request</span><span style="line-height:1.5;color:#008000;"> </span>lastModified: {},
ajax: <span style="line-height:1.5;color:#0000ff;">function</span>( type, url, data, ret, ifModified ) {
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If only a single argument was passed in,</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> assume that it is a object of key/value pairs</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( !url ) {
        ret = type.complete;
        <span style="line-height:1.5;color:#0000ff;">var</span> success = type.success;
        <span style="line-height:1.5;color:#0000ff;">var</span> error = type.error;
        data = type.data;
        url = type.url;
        type = type.type;
    }
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Watch for a new set of requests</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( ! jQuery.active++ )
        jQuery.event.trigger( "ajaxStart" );
    <span style="line-height:1.5;color:#0000ff;">var</span> requestDone = <span style="line-height:1.5;color:#0000ff;">false</span>;
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Create the request object</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">var</span> xml = <span style="line-height:1.5;color:#0000ff;">new</span> XMLHttpRequest();
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Open the socket</span><span style="line-height:1.5;color:#008000;"> </span>    xml.open(type || "GET", url, <span style="line-height:1.5;color:#0000ff;">true</span>);
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Set the correct header, if data is being sent</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( data )
        xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Set the If-Modified-Since header, if ifModified mode.</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( ifModified )
        xml.setRequestHeader("If-Modified-Since",
            jQuery.lastModified[url] || "Thu, 01 Jan 1970 00:00:00 GMT" );
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Set header so calling script knows that it's an XMLHttpRequest</span><span style="line-height:1.5;color:#008000;"> </span>    xml.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Make sure the browser sends the right content length</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( xml.overrideMimeType )
        xml.setRequestHeader("Connection", "close");
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Wait for a response to come back</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">var</span> onreadystatechange = <span style="line-height:1.5;color:#0000ff;">function</span>(istimeout){
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> The transfer is complete and the data is available, or the request timed out</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">if</span> ( xml &amp;&amp; (xml.readyState == 4 || istimeout == "timeout") ) {
            requestDone = <span style="line-height:1.5;color:#0000ff;">true</span>;
            <span style="line-height:1.5;color:#0000ff;">var</span> status = jQuery.httpSuccess( xml ) &amp;&amp; istimeout != "timeout" ?
                ifModified &amp;&amp; jQuery.httpNotModified( xml, url ) ? "notmodified" : "success" : "error";
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Make sure that the request was successful or notmodified</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">if</span> ( status != "error" ) {
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Cache Last-Modified header, if ifModified mode.</span><span style="line-height:1.5;color:#008000;"> </span>                <span style="line-height:1.5;color:#0000ff;">var</span> modRes = xml.getResponseHeader("Last-Modified");
                <span style="line-height:1.5;color:#0000ff;">if</span> ( ifModified &amp;&amp; modRes ) jQuery.lastModified[url] = modRes;
                 
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If a local callback was specified, fire it</span><span style="line-height:1.5;color:#008000;"> </span>                <span style="line-height:1.5;color:#0000ff;">if</span> ( success ) success( xml, status );
                 
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Fire the global callback</span><span style="line-height:1.5;color:#008000;"> </span>                jQuery.event.trigger( "ajaxSuccess" );
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Otherwise, the request was not successful</span><span style="line-height:1.5;color:#008000;"> </span>            } <span style="line-height:1.5;color:#0000ff;">else</span> {
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If a local callback was specified, fire it</span><span style="line-height:1.5;color:#008000;"> </span>                <span style="line-height:1.5;color:#0000ff;">if</span> ( error ) error( xml, status );
                 
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Fire the global callback</span><span style="line-height:1.5;color:#008000;"> </span>                jQuery.event.trigger( "ajaxError" );
            }
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> The request was completed</span><span style="line-height:1.5;color:#008000;"> </span>            jQuery.event.trigger( "ajaxComplete" );
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Handle the global AJAX counter</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">if</span> ( ! --jQuery.active )
                jQuery.event.trigger( "ajaxStop" );
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Process result</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">if</span> ( ret ) ret(xml, status);
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Stop memory leaks</span><span style="line-height:1.5;color:#008000;"> </span>            xml.onreadystatechange = <span style="line-height:1.5;color:#0000ff;">function</span>(){};
            xml = <span style="line-height:1.5;color:#0000ff;">null</span>;
             
        }
    };
    xml.onreadystatechange = onreadystatechange;
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Timeout checker</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span>(jQuery.timeout &gt; 0)
        setTimeout(<span style="line-height:1.5;color:#0000ff;">function</span>(){
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Check to see if the request is still happening</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">if</span> (xml) {
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Cancel the request</span><span style="line-height:1.5;color:#008000;"> </span>                xml.abort();
                <span style="line-height:1.5;color:#0000ff;">if</span> ( !requestDone ) onreadystatechange( "timeout" );
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Clear from memory</span><span style="line-height:1.5;color:#008000;"> </span>                xml = <span style="line-height:1.5;color:#0000ff;">null</span>;
            }
        }, jQuery.timeout);
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Send the data</span><span style="line-height:1.5;color:#008000;"> </span>    xml.send(data);
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348050.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">从第一行开始看。如果只传入了一个参数则认为传入的是一个参数对象，获取对象传递的参数。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第二行判断是否开始一批新的请求，如果是的话则触发ajaxStart事件。注意这里trigger的使用，触发了全局的事件，所有的监听者都会监听。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">开始请求并设置请求的参数，定义请求状态变化时的响应方法并绑定。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">如果给定了超时时间设置设置超时监听方法。在超时时进行超时处理。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">发送ajax请求。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">先分析超时响应的方法代码，判断当前的请求对象是否存在，如果存在则退出当前请求。调用onreadystatechange('timeout')。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">onreadystatechange方法。获取请求状态，如果请求状态是成功的则设置请求地址的最近修改时间，如果设置了请求完成的处理方法则调用该方法，触发全局的ajaxSuccess事件。如果没有成功，如果设置了失败的处理方法则调用该方法，触发全局的ajaxError事件。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">触发全局的ajaxComplete事件。如果再没有了ajax请求则触发ajaxStop事件。 后两个代码清处理内存问题。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">向上看，还有简单代码：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> timeout (ms)</span><span style="line-height:1.5;color:#008000;"> </span>timeout: 0,
ajaxTimeout: <span style="line-height:1.5;color:#0000ff;">function</span>(timeout) {
    jQuery.timeout = timeout;
},</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348050.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">再向上看：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">get: <span style="line-height:1.5;color:#0000ff;">function</span>( url, data, callback, type, ifModified ) {
    <span style="line-height:1.5;color:#0000ff;">if</span> ( data.constructor == Function ) {
        type = callback;
        callback = data;
        data = <span style="line-height:1.5;color:#0000ff;">null</span>;
    }
     
    <span style="line-height:1.5;color:#0000ff;">if</span> ( data ) url += "?" + jQuery.param(data);
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Build and start the HTTP Request</span><span style="line-height:1.5;color:#008000;"> </span>    jQuery.ajax( "GET", url, <span style="line-height:1.5;color:#0000ff;">null</span>, <span style="line-height:1.5;color:#0000ff;">function</span>(r, status) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( callback ) callback( jQuery.httpData(r,type), status );
    }, ifModified);
},
getIfModified: <span style="line-height:1.5;color:#0000ff;">function</span>( url, data, callback, type ) {
    jQuery.get(url, data, callback, type, 1);
},
getScript: <span style="line-height:1.5;color:#0000ff;">function</span>( url, data, callback ) {
    jQuery.get(url, data, callback, "script");
},
post: <span style="line-height:1.5;color:#0000ff;">function</span>( url, data, callback, type ) {
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Build and start the HTTP Request</span><span style="line-height:1.5;color:#008000;"> </span>    jQuery.ajax( "POST", url, jQuery.param(data), <span style="line-height:1.5;color:#0000ff;">function</span>(r, status) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( callback ) callback( jQuery.httpData(r,type), status );
    });
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348050.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这几个方法是对ajax方法的封装。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">对jQuery.fn上ajax相关方法的扩展以及ie兼容的XMLHttpRequest创建代码不在介绍。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.fn.loadIfModified = <span style="line-height:1.5;color:#0000ff;">function</span>( url, params, callback ) {
    <span style="line-height:1.5;color:#0000ff;">this</span>.load( url, params, callback, 1 );
};
jQuery.fn.load = <span style="line-height:1.5;color:#0000ff;">function</span>( url, params, callback, ifModified ) {
    <span style="line-height:1.5;color:#0000ff;">if</span> ( url.constructor == Function )
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.bind("load", url);
    callback = callback || <span style="line-height:1.5;color:#0000ff;">function</span>(){};
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Default to a GET request</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">var</span> type = "GET";
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If the second parameter was provided</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( params ) {
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If it's a function</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">if</span> ( params.constructor == Function ) {
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> We assume that it's the callback</span><span style="line-height:1.5;color:#008000;"> </span>            callback = params;
            params = <span style="line-height:1.5;color:#0000ff;">null</span>;
             
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Otherwise, build a param string</span><span style="line-height:1.5;color:#008000;"> </span>        } <span style="line-height:1.5;color:#0000ff;">else</span> {
            params = jQuery.param( params );
            type = "POST";
        }
    }
     
    <span style="line-height:1.5;color:#0000ff;">var</span> self = <span style="line-height:1.5;color:#0000ff;">this</span>;
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Request the remote document</span><span style="line-height:1.5;color:#008000;"> </span>    jQuery.ajax( type, url, params,<span style="line-height:1.5;color:#0000ff;">function</span>(res, status){
         
        <span style="line-height:1.5;color:#0000ff;">if</span> ( status == "success" || !ifModified &amp;&amp; status == "notmodified" ) {
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Inject the HTML into all the matched elements</span><span style="line-height:1.5;color:#008000;"> </span>            self.html(res.responseText).each( callback, [res.responseText, status] );
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Execute all the scripts inside of the newly-injected HTML</span><span style="line-height:1.5;color:#008000;"> </span>            $("script", self).each(<span style="line-height:1.5;color:#0000ff;">function</span>(){
                <span style="line-height:1.5;color:#0000ff;">if</span> ( <span style="line-height:1.5;color:#0000ff;">this</span>.src )
                    $.getScript( <span style="line-height:1.5;color:#0000ff;">this</span>.src );
                <span style="line-height:1.5;color:#0000ff;">else</span>                     eval.call( window, <span style="line-height:1.5;color:#0000ff;">this</span>.text || <span style="line-height:1.5;color:#0000ff;">this</span>.textContent || <span style="line-height:1.5;color:#0000ff;">this</span>.innerHTML || "" );
            });
        } <span style="line-height:1.5;color:#0000ff;">else</span>             callback.apply( self, [res.responseText, status] );
    }, ifModified);
     
    <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>;
};</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348050.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">如上代码定义了jQuery对象的ajax请求模式的load方法。load的实现里需要注意ajax请求成功以后的响应函数的实现。它会把响应内容赋值到当前jQuery对象关联的DOM元素的innerHTML属性上，并对其进行遍历调用指定的回调函数，并对新添加的脚本进行执行和获取。</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-twelve.html]]></link>
<title><![CDATA[（十二）jQuery内置特效的实现]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 16:14:58 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">jQuery内置特效的实现用了两段extend代码。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第一段jQuery.fn.extend扩展了jQuery.fn上的常见特效方法。其中重要的animate方法。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第二段jQuery.extend 定义了重要的fx方法、speed方法。支持animate的实现。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第一段jQuery.fn.extend</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.fn.extend({
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> overwrite the old show method</span><span style="line-height:1.5;color:#008000;"> </span>    _show: jQuery.fn.show,
    show: <span style="line-height:1.5;color:#0000ff;">function</span>(speed,callback){
        <span style="line-height:1.5;color:#0000ff;">return</span> speed ? <span style="line-height:1.5;color:#0000ff;">this</span>.animate({
            height: "show", width: "show", opacity: "show"
        }, speed, callback) : <span style="line-height:1.5;color:#0000ff;">this</span>._show();
    },
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Overwrite the old hide method</span><span style="line-height:1.5;color:#008000;"> </span>    _hide: jQuery.fn.hide,
    hide: <span style="line-height:1.5;color:#0000ff;">function</span>(speed,callback){
        <span style="line-height:1.5;color:#0000ff;">return</span> speed ? <span style="line-height:1.5;color:#0000ff;">this</span>.animate({
            height: "hide", width: "hide", opacity: "hide"
        }, speed, callback) : <span style="line-height:1.5;color:#0000ff;">this</span>._hide();
    },
    slideDown: <span style="line-height:1.5;color:#0000ff;">function</span>(speed,callback){
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.animate({height: "show"}, speed, callback);
    },
    slideUp: <span style="line-height:1.5;color:#0000ff;">function</span>(speed,callback){
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.animate({height: "hide"}, speed, callback);
    },
    slideToggle: <span style="line-height:1.5;color:#0000ff;">function</span>(speed,callback){
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.each(<span style="line-height:1.5;color:#0000ff;">function</span>(){
            <span style="line-height:1.5;color:#0000ff;">var</span> state = $(<span style="line-height:1.5;color:#0000ff;">this</span>).is(":hidden") ? "show" : "hide";
            $(<span style="line-height:1.5;color:#0000ff;">this</span>).animate({height: state}, speed, callback);
        });
    },
    fadeIn: <span style="line-height:1.5;color:#0000ff;">function</span>(speed,callback){
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.animate({opacity: "show"}, speed, callback);
    },
    fadeOut: <span style="line-height:1.5;color:#0000ff;">function</span>(speed,callback){
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.animate({opacity: "hide"}, speed, callback);
    },
    fadeTo: <span style="line-height:1.5;color:#0000ff;">function</span>(speed,to,callback){
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.animate({opacity: to}, speed, callback);
    },
    animate: <span style="line-height:1.5;color:#0000ff;">function</span>(prop,speed,callback) {
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.queue(<span style="line-height:1.5;color:#0000ff;">function</span>(){
         
            <span style="line-height:1.5;color:#0000ff;">this</span>.curAnim = prop;
             
            <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> p <span style="line-height:1.5;color:#0000ff;">in</span> prop ) {
                <span style="line-height:1.5;color:#0000ff;">var</span> e = <span style="line-height:1.5;color:#0000ff;">new</span> jQuery.fx( <span style="line-height:1.5;color:#0000ff;">this</span>, jQuery.speed(speed,callback), p );
                <span style="line-height:1.5;color:#0000ff;">if</span> ( prop[p].constructor == Number )
                    e.custom( e.cur(), prop[p] );
                <span style="line-height:1.5;color:#0000ff;">else</span>                     e[ prop[p] ]( prop );
            }
             
        });
    },
    queue: <span style="line-height:1.5;color:#0000ff;">function</span>(type,fn){
        <span style="line-height:1.5;color:#0000ff;">if</span> ( !fn ) {
            fn = type;
            type = "fx";
        }
     
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.each(<span style="line-height:1.5;color:#0000ff;">function</span>(){
            <span style="line-height:1.5;color:#0000ff;">if</span> ( !<span style="line-height:1.5;color:#0000ff;">this</span>.queue )
                <span style="line-height:1.5;color:#0000ff;">this</span>.queue = {};
     
            <span style="line-height:1.5;color:#0000ff;">if</span> ( !<span style="line-height:1.5;color:#0000ff;">this</span>.queue[type] )
                <span style="line-height:1.5;color:#0000ff;">this</span>.queue[type] = [];
     
            <span style="line-height:1.5;color:#0000ff;">this</span>.queue[type].push( fn );
         
            <span style="line-height:1.5;color:#0000ff;">if</span> ( <span style="line-height:1.5;color:#0000ff;">this</span>.queue[type].length == 1 )
                fn.apply(<span style="line-height:1.5;color:#0000ff;">this</span>);
        });
    }
});</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348048.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">animate之前的函数实现都是一句话调用了animate实现，使其支持特效。show和hide重写了原先定义的show和hide方法。animte调用了queue方法将构造的特效处理方法进行了排队。animate中调用的fx方法和 speed方法均定义在第二段jQuery.extend代码中：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.extend({
    setAuto: <span style="line-height:1.5;color:#0000ff;">function</span>(e,p) {
        ...
    },
     
    speed: <span style="line-height:1.5;color:#0000ff;">function</span>(s,o) {
        o = o || {};
         
        <span style="line-height:1.5;color:#0000ff;">if</span> ( o.constructor == Function )
            o = { complete: o };
         
        <span style="line-height:1.5;color:#0000ff;">var</span> ss = { slow: 600, fast: 200 };
        o.duration = (s &amp;&amp; s.constructor == Number ? s : ss[s]) || 400;
     
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Queueing</span><span style="line-height:1.5;color:#008000;"> </span>        o.oldComplete = o.complete;
        o.complete = <span style="line-height:1.5;color:#0000ff;">function</span>(){
            jQuery.dequeue(<span style="line-height:1.5;color:#0000ff;">this</span>, "fx");
            <span style="line-height:1.5;color:#0000ff;">if</span> ( o.oldComplete &amp;&amp; o.oldComplete.constructor == Function )
                o.oldComplete.apply( <span style="line-height:1.5;color:#0000ff;">this</span> );
        };
     
        <span style="line-height:1.5;color:#0000ff;">return</span> o;
    },
     
    queue: {},
     
    dequeue: <span style="line-height:1.5;color:#0000ff;">function</span>(elem,type){
        type = type || "fx";
     
        <span style="line-height:1.5;color:#0000ff;">if</span> ( elem.queue &amp;&amp; elem.queue[type] ) {
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Remove self</span><span style="line-height:1.5;color:#008000;"> </span>            elem.queue[type].shift();
     
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Get next function</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">var</span> f = elem.queue[type][0];
         
            <span style="line-height:1.5;color:#0000ff;">if</span> ( f ) f.apply( elem );
        }
    },
    <span style="line-height:1.5;color:#008000;">/*</span><span style="line-height:1.5;color:#008000;">      * I originally wrote fx() as a clone of moo.fx and in the process
     * of making it small in size the code became illegible to sane
     * people. You've been warned.
     </span><span style="line-height:1.5;color:#008000;">*/</span>      
    fx: <span style="line-height:1.5;color:#0000ff;">function</span>( elem, options, prop ){
        ...
    }
});</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348048.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">setAuto这个函数在这里出现显得不明所以，之前看过的代码里没有对setAuto的引用。向后查找一下发现在fx中调用了setAuto。所以setAtuo的理解要结合fx理解。剩余的speed 和 dequeue很好理解。dequeue被speed中定义的complete方法调用。用于将完成的特效处理方法移除。queue不能和上面jQuery.fn.extend中的queue相关代码联系。这里的queue是定义与jQuery之上的。我们知道jQuery通过13秒的间隔来执行一个指定的函数。没错这个queue就是为这个13秒间隔执行的函数提供队列的。speed方法控制了特效执行的速度并添加了对特效完成时的清理工作。注意到dequeue和queue的实现，两个函数都支持传递type，但是在所有的调用里。queue由于只传入了一个参数所以type='fx'，而dequeue则指明为'fx'。上面代码中定义的queue属性是不是略显多余，因为queue和dequeue操作的都是jQuery对象关联的DOM元素上的queue属性中的内容？</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">看一下setAuto和fx的代码：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">setAuto: <span style="line-height:1.5;color:#0000ff;">function</span>(e,p) {
    <span style="line-height:1.5;color:#0000ff;">if</span> ( e.notAuto ) <span style="line-height:1.5;color:#0000ff;">return</span>;
    <span style="line-height:1.5;color:#0000ff;">if</span> ( p == "height" &amp;&amp; e.scrollHeight != parseInt(jQuery.curCSS(e,p)) ) <span style="line-height:1.5;color:#0000ff;">return</span>;
    <span style="line-height:1.5;color:#0000ff;">if</span> ( p == "width" &amp;&amp; e.scrollWidth != parseInt(jQuery.curCSS(e,p)) ) <span style="line-height:1.5;color:#0000ff;">return</span>;
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Remember the original height</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">var</span> a = e.style[p];
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Figure out the size of the height right now</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">var</span> o = jQuery.curCSS(e,p,1);
    <span style="line-height:1.5;color:#0000ff;">if</span> ( p == "height" &amp;&amp; e.scrollHeight != o ||
        p == "width" &amp;&amp; e.scrollWidth != o ) <span style="line-height:1.5;color:#0000ff;">return</span>;
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Set the height to auto</span><span style="line-height:1.5;color:#008000;"> </span>    e.style[p] = e.currentStyle ? "" : "auto";
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> See what the size of "auto" is</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">var</span> n = jQuery.curCSS(e,p,1);
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Revert back to the original size</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( o != n &amp;&amp; n != "auto" ) {
        e.style[p] = a;
        e.notAuto = <span style="line-height:1.5;color:#0000ff;">true</span>;
    }
}
 
fx: <span style="line-height:1.5;color:#0000ff;">function</span>( elem, options, prop ){
    <span style="line-height:1.5;color:#0000ff;">var</span> z = <span style="line-height:1.5;color:#0000ff;">this</span>;
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> The users options</span><span style="line-height:1.5;color:#008000;"> </span>    z.o = {
        duration: options.duration || 400,
        complete: options.complete,
        step: options.step
    };
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> The element</span><span style="line-height:1.5;color:#008000;"> </span>    z.el = elem;
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> The styles</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">var</span> y = z.el.style;
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Simple function for setting a style value</span><span style="line-height:1.5;color:#008000;"> </span>    z.a = <span style="line-height:1.5;color:#0000ff;">function</span>(){
        <span style="line-height:1.5;color:#0000ff;">if</span> ( options.step )
            options.step.apply( elem, [ z.now ] );
        <span style="line-height:1.5;color:#0000ff;">if</span> ( prop == "opacity" ) {
            <span style="line-height:1.5;color:#0000ff;">if</span> (z.now == 1) z.now = 0.9999;
            <span style="line-height:1.5;color:#0000ff;">if</span> (window.ActiveXObject)
                y.filter = "alpha(opacity=" + z.now*100 + ")";
            <span style="line-height:1.5;color:#0000ff;">else</span>                 y.opacity = z.now;
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> My hate for IE will never die</span><span style="line-height:1.5;color:#008000;"> </span>        } <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( parseInt(z.now) )
            y[prop] = parseInt(z.now) + "px";
             
        y.display = "block";
    };
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Figure out the maximum number to run to</span><span style="line-height:1.5;color:#008000;"> </span>    z.max = <span style="line-height:1.5;color:#0000ff;">function</span>(){
        <span style="line-height:1.5;color:#0000ff;">return</span> parseFloat( jQuery.css(z.el,prop) );
    };
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Get the current size</span><span style="line-height:1.5;color:#008000;"> </span>    z.cur = <span style="line-height:1.5;color:#0000ff;">function</span>(){
        <span style="line-height:1.5;color:#0000ff;">var</span> r = parseFloat( jQuery.curCSS(z.el, prop) );
        <span style="line-height:1.5;color:#0000ff;">return</span> r &amp;&amp; r &gt; -10000 ? r : z.max();
    };
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Start an animation from one number to another</span><span style="line-height:1.5;color:#008000;"> </span>    z.custom = <span style="line-height:1.5;color:#0000ff;">function</span>(from,to){
        z.startTime = (<span style="line-height:1.5;color:#0000ff;">new</span> Date()).getTime();
        z.now = from;
        z.a();
        z.timer = setInterval(<span style="line-height:1.5;color:#0000ff;">function</span>(){
            z.step(from, to);
        }, 13);
    };
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Simple 'show' function</span><span style="line-height:1.5;color:#008000;"> </span>    z.show = <span style="line-height:1.5;color:#0000ff;">function</span>( p ){
        <span style="line-height:1.5;color:#0000ff;">if</span> ( !z.el.orig ) z.el.orig = {};
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Remember where we started, so that we can go back to it later</span><span style="line-height:1.5;color:#008000;"> </span>        z.el.orig[prop] = <span style="line-height:1.5;color:#0000ff;">this</span>.cur();
        z.custom( 0, z.el.orig[prop] );
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Stupid IE, look what you made me do</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">if</span> ( prop != "opacity" )
            y[prop] = "1px";
    };
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Simple 'hide' function</span><span style="line-height:1.5;color:#008000;"> </span>    z.hide = <span style="line-height:1.5;color:#0000ff;">function</span>(){
        <span style="line-height:1.5;color:#0000ff;">if</span> ( !z.el.orig ) z.el.orig = {};
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Remember where we started, so that we can go back to it later</span><span style="line-height:1.5;color:#008000;"> </span>        z.el.orig[prop] = <span style="line-height:1.5;color:#0000ff;">this</span>.cur();
        z.o.hide = <span style="line-height:1.5;color:#0000ff;">true</span>;
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Begin the animation</span><span style="line-height:1.5;color:#008000;"> </span>        z.custom(z.el.orig[prop], 0);
    };
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> IE has trouble with opacity if it does not have layout</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( jQuery.browser.msie &amp;&amp; !z.el.currentStyle.hasLayout )
        y.zoom = "1";
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Remember  the overflow of the element</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( !z.el.oldOverlay )
        z.el.oldOverflow = jQuery.css( z.el, "overflow" );
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Make sure that nothing sneaks out</span><span style="line-height:1.5;color:#008000;"> </span>    y.overflow = "hidden";
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Each step of an animation</span><span style="line-height:1.5;color:#008000;"> </span>    z.step = <span style="line-height:1.5;color:#0000ff;">function</span>(firstNum, lastNum){
        <span style="line-height:1.5;color:#0000ff;">var</span> t = (<span style="line-height:1.5;color:#0000ff;">new</span> Date()).getTime();
        <span style="line-height:1.5;color:#0000ff;">if</span> (t &gt; z.o.duration + z.startTime) {
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Stop the timer</span><span style="line-height:1.5;color:#008000;"> </span>            clearInterval(z.timer);
            z.timer = <span style="line-height:1.5;color:#0000ff;">null</span>;
            z.now = lastNum;
            z.a();
            z.el.curAnim[ prop ] = <span style="line-height:1.5;color:#0000ff;">true</span>;
             
            <span style="line-height:1.5;color:#0000ff;">var</span> done = <span style="line-height:1.5;color:#0000ff;">true</span>;
            <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i <span style="line-height:1.5;color:#0000ff;">in</span> z.el.curAnim )
                <span style="line-height:1.5;color:#0000ff;">if</span> ( z.el.curAnim[i] !== <span style="line-height:1.5;color:#0000ff;">true</span> )
                    done = <span style="line-height:1.5;color:#0000ff;">false</span>;
                     
            <span style="line-height:1.5;color:#0000ff;">if</span> ( done ) {
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Reset the overflow</span><span style="line-height:1.5;color:#008000;"> </span>                y.overflow = z.el.oldOverflow;
             
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Hide the element if the "hide" operation was done</span><span style="line-height:1.5;color:#008000;"> </span>                <span style="line-height:1.5;color:#0000ff;">if</span> ( z.o.hide )
                    y.display = 'none';
                 
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Reset the property, if the item has been hidden</span><span style="line-height:1.5;color:#008000;"> </span>                <span style="line-height:1.5;color:#0000ff;">if</span> ( z.o.hide ) {
                    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> p <span style="line-height:1.5;color:#0000ff;">in</span> z.el.curAnim ) {
                        y[ p ] = z.el.orig[p] + ( p == "opacity" ? "" : "px" );
                        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> set its height and/or width to auto</span><span style="line-height:1.5;color:#008000;"> </span>                        <span style="line-height:1.5;color:#0000ff;">if</span> ( p == 'height' || p == 'width' )
                            jQuery.setAuto( z.el, p );
                    }
                }
            }
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If a callback was provided, execute it</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">if</span>( done &amp;&amp; z.o.complete &amp;&amp; z.o.complete.constructor == Function )
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Execute the complete function</span><span style="line-height:1.5;color:#008000;"> </span>                z.o.complete.apply( z.el );
        } <span style="line-height:1.5;color:#0000ff;">else</span> {
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Figure out where in the animation we are and set the number</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">var</span> p = (t - <span style="line-height:1.5;color:#0000ff;">this</span>.startTime) / z.o.duration;
            z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Perform the next step of the animation</span><span style="line-height:1.5;color:#008000;"> </span>            z.a();
        }
    };
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348048.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">先看fx，fx第一行代码 var z = this; 这里的this指针指向的是 new jQuery.fx产生的对象。接下来三行代码定义了z上的o属性(options)、z上的el属性(缓存elem引用)和y变量(缓存elem的style属性)。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">接下来定义了z上的a函数。a函数主要实现通过指定的方法或者默认的方式设置特效作用的属性的值。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">接下来在z.max 和 z.cur 用于获取当前元素指定的属性的最大值和现在的值。通过查找max和cur的调用位置可以看出两个函数的实现</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">z.custom相当于fx的核心、驱动器。因为我们在这里看到了window.setInterval(function,13)的代码。z.custom实现：记录当前的开始时间。设置z.now为from值。调用z.a()将z.now的值设置到元素上。开始触发器执行z.step()方法。为什么在开始触发器之前要调用一次z.a()呢？因为from的值可能不是元素当前的值嘛。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">z.show 和 z.hide方法实现了对响应需要显示和隐藏的特效进行了封装，默认传递了一个为0的参数。并对旧的属性值进行了记录。刚开始我一下也没反映过来show和hide调用位置。在后面判断了z.o.hide，本来我还以为这俩方法没有被调用过的。直到我看了几遍判断z.o.hide的代码后觉得看看前面animate附近的代码定义时才发现对show和hide的调用位置。注意animate中代码实现判断p不为Number所执行的代码。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">接下来是对ie的一个兼容处理和一个对overflow的处理，防止在特效时元素中的内容超出容器之外显示。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">代码量稍多的step方法。获取当前时间。如果没有超出特效的执行时间那么进行下一步特效，计算下一步的值并设置属性值。如果超出了特效执行时间停止触发器，清理timer，将z.now设置为最终值并调用z.a()设置到元素属性上。设置z.el.curAnim[prop] = true 标记对此属性的特效已完成。这里要注意到当对el一次特效处理设置了多个属性时，会每个属性产生一个z(jQuery.fx对象实例)，所有z上的el都是同一个，这也表明每个z处理一个单一的属性。检查是否真的完成所有属性的特效处理。如果完成了所有的特效处理还原overflow属性，如果是hide特效则在第一次if检查时设置display属性为none，第二次if检查时将元素的特效属性的原值设置回去。如果完成了特效处理同时设置了回调函数则执行回调函数。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">在最后完成时检查z.o.hiden为啥要写两个一样的if呢？</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-eleven.html]]></link>
<title><![CDATA[（十一）jQuery.extend代码段]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 16:10:51 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">在jQuery.init();代码之后有一段jQuery.extend代码。这一段属于对jQuery基本功能的扩展。代码如下：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.init();jQuery.fn.extend({
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> We're overriding the old toggle function, so</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> remember it for later</span><span style="line-height:1.5;color:#008000;"> </span>    _toggle: jQuery.fn.toggle,
    toggle: <span style="line-height:1.5;color:#0000ff;">function</span>(a,b) {
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If two functions are passed in, we're</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> toggling on a click</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">return</span> a &amp;&amp; b &amp;&amp; a.constructor == Function &amp;&amp; b.constructor == Function ? <span style="line-height:1.5;color:#0000ff;">this</span>.click(<span style="line-height:1.5;color:#0000ff;">function</span>(e){
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Figure out which function to execute</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">this</span>.last = <span style="line-height:1.5;color:#0000ff;">this</span>.last == a ? b : a;
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Make sure that clicks stop</span><span style="line-height:1.5;color:#008000;"> </span>            e.preventDefault();
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> and execute the function</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.last.apply( <span style="line-height:1.5;color:#0000ff;">this</span>, [e] ) || <span style="line-height:1.5;color:#0000ff;">false</span>;
        }) :
         
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Otherwise, execute the old toggle function</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">this</span>._toggle.apply( <span style="line-height:1.5;color:#0000ff;">this</span>, arguments );
    },
    hover: <span style="line-height:1.5;color:#0000ff;">function</span>(f,g) {
         
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> A private function for haandling mouse 'hovering'</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">function</span> handleHover(e) {
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Check if mouse(over|out) are still within the same parent element</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">var</span> p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
     
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Traverse up the tree</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">while</span> ( p &amp;&amp; p != <span style="line-height:1.5;color:#0000ff;">this</span> ) p = p.parentNode;
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If we actually just moused on to a sub-element, ignore it</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">if</span> ( p == <span style="line-height:1.5;color:#0000ff;">this</span> ) <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">false</span>;
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Execute the right function</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">return</span> (e.type == "mouseover" ? f : g).apply(<span style="line-height:1.5;color:#0000ff;">this</span>, [e]);
        }
         
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Bind the function to the two event listeners</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.mouseover(handleHover).mouseout(handleHover);
    },
    ready: <span style="line-height:1.5;color:#0000ff;">function</span>(f) {
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If the DOM is already ready</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">if</span> ( jQuery.isReady )
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Execute the function immediately</span><span style="line-height:1.5;color:#008000;"> </span>            f.apply( document );
             
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Otherwise, remember the function for later</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">else</span> {
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Add the function to the wait list</span><span style="line-height:1.5;color:#008000;"> </span>            jQuery.readyList.push( f );
        }
     
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>;
    }
});</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348045.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">最后面ready函数是添加对页面加载完成函数的代码。如果页面已经加载则立即调用当前设置的监听函数，没有则将当前方法加入到ready事件的监听列表中。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">返回头从第一个函数定义开始看。第一个函数toggle，通过之前对_toggle的注释中认为这个toggle对原先定义的toggle函数进行了重定义。看代码实现是扩展了原有toggle函数的功能。那么，原先toggle函数是实现了什么功能，这里又添加了什么功能的支持呢？通过观察嗲吗我们可以看到，这里的toggle函数对_toggle原有功能扩充代码即为return 这一行的代码。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第二个hover函数，其上的handleHover是响应mouseover和mouseout的方法。注意其实现中的</p>
<pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;color:#333333;font-size:14px;line-height:25px;text-align:left;background-color:#ffffff;">while ( p &amp;&amp; p != this ) p = p.parentNode;</pre><p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这一段代码，这段代码实现了当鼠标停留在元素内部的元素上时，依旧当作停留在当前元素上的处理。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">分析完这一段代码，jQuery的基本功能算是已经完备了。剩下后面的代码是对jQuery进行的功能扩充（基本特效代码和Ajax功能支持），这些代码可以看作如何对jQuery进行功能扩展的学习。现在看完了jQuery的基本功能，思考一下学到的东西。</p>
<ul style="list-style-position:initial;list-style-image:initial;margin-top:10px;margin-right:10px;margin-bottom:10px;margin-left:45px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">jQuery对CSS类和属性的操作</li>
<li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">jQuery强大的DOM选择器</li>
<li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">jQuery如果判断浏览器版本，和浏览器当前使用的盒子模型。</li>
<li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">jQuery跨浏览器页面加载完成事件的监听。</li>
<li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">jQuery的事件模型</li>
<li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">jQuery extend、each的实现</li>
</ul>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-ten.html]]></link>
<title><![CDATA[（十）jQuery.extend代码段]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 16:08:43 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">查看jQuery.css实现代码发现css调用了curCss，而这个curCss调用了swap方法，swap方法没有调用其它方法。于是</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">swap: <span style="line-height:1.5;color:#0000ff;">function</span>(e,o,f) {
    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i <span style="line-height:1.5;color:#0000ff;">in</span> o ) {
        e.style["old"+i] = e.style[i];
        e.style[i] = o[i];
    }
    f.apply( e, [] );
    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i <span style="line-height:1.5;color:#0000ff;">in</span> o )
        e.style[i] = e.style["old"+i];
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348044.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">swap方法实现的是将e上的原有属性保存在old前缀的属性上，然后调用指定的函数f，最后还原e上的原有属性。接下来看curCSS的实现</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">curCSS: <span style="line-height:1.5;color:#0000ff;">function</span>(elem, prop, force) {
    <span style="line-height:1.5;color:#0000ff;">var</span> ret;
    <span style="line-height:1.5;color:#0000ff;">if</span> (!force &amp;&amp; elem.style[prop]) {
        ret = elem.style[prop];
    } <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> (elem.currentStyle) {
        <span style="line-height:1.5;color:#0000ff;">var</span> newProp = prop.replace(/\-(\w)/g,<span style="line-height:1.5;color:#0000ff;">function</span>(m,c){<span style="line-height:1.5;color:#0000ff;">return</span> c.toUpperCase()});
        ret = elem.currentStyle[prop] || elem.currentStyle[newProp];
    } <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> (document.defaultView &amp;&amp; document.defaultView.getComputedStyle) {
        prop = prop.replace(/([A-Z])/g,"-$1").toLowerCase();
        <span style="line-height:1.5;color:#0000ff;">var</span> cur = document.defaultView.getComputedStyle(elem, <span style="line-height:1.5;color:#0000ff;">null</span>);
        <span style="line-height:1.5;color:#0000ff;">if</span> ( cur )
            ret = cur.getPropertyValue(prop);
        <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( prop == 'display' )
            ret = 'none';
        <span style="line-height:1.5;color:#0000ff;">else</span>             jQuery.swap(elem, { display: 'block' }, <span style="line-height:1.5;color:#0000ff;">function</span>() {
                ret = document.defaultView.getComputedStyle(<span style="line-height:1.5;color:#0000ff;">this</span>,<span style="line-height:1.5;color:#0000ff;">null</span>).getPropertyValue(prop);
            });
    }
     
    <span style="line-height:1.5;color:#0000ff;">return</span> ret;
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348044.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">css操作的实现个浏览器之间会有些许区别。主要是ie和非ie浏览器。在ie下使用elem.style[property name] 或者 elem.currentStyle[property name]获取，在非ie浏览器下通过elem.currentStyle[property name]或者getComputedStyle获取。在ie下，ie9以后版本开始了对document.defaultView的支持。curCSS代码结构分为三个if判断结构</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">当force不为true时并且elem.style[prop]存在，则取elem.style[prop]这个值</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">当elem.currentStyle存在时，ie下需要将 '-' 后第一个字母大写，非ie则不需要。于是这里对大写和不大写的值都进行了获取。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">当document.defaultView存在且document.defaultView.getComputedStyle存在时。将prop字符串 '-' 后第一个字母转换为小写模式。如果getComputedStyle没有返回正确值时，如果获取的是display属性则返回none。否则设置当前元素display = 'block'再获取指定的css值。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">接下来看jQuery.css定义：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">css: <span style="line-height:1.5;color:#0000ff;">function</span>(e,p) {
    <span style="line-height:1.5;color:#0000ff;">if</span> ( p == "height" || p == "width" ) {
        <span style="line-height:1.5;color:#0000ff;">var</span> old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
        <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i <span style="line-height:1.5;color:#0000ff;">in</span> d ) {
            old["padding" + d[i]] = 0;
            old["border" + d[i] + "Width"] = 0;
        }
        jQuery.swap( e, old, <span style="line-height:1.5;color:#0000ff;">function</span>() {
            <span style="line-height:1.5;color:#0000ff;">if</span> (jQuery.css(e,"display") != "none") {
                oHeight = e.offsetHeight;
                oWidth = e.offsetWidth;
            } <span style="line-height:1.5;color:#0000ff;">else</span> {
                e = $(e.cloneNode(<span style="line-height:1.5;color:#0000ff;">true</span>)).css({
                    visibility: "hidden", position: "absolute", display: "block"
                }).prependTo("body")[0];
                oHeight = e.clientHeight;
                oWidth = e.clientWidth;
                 
                e.parentNode.removeChild(e);
            }
        });
        <span style="line-height:1.5;color:#0000ff;">return</span> p == "height" ? oHeight : oWidth;
    } <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( p == "opacity" &amp;&amp; jQuery.browser.msie )
        <span style="line-height:1.5;color:#0000ff;">return</span> parseFloat( jQuery.curCSS(e,"filter").replace(/[^0-9.]/,"") ) || 1;
    <span style="line-height:1.5;color:#0000ff;">return</span> jQuery.curCSS( e, p );
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348044.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这个css函数获取元素e上p指定的css属性值。这个函数视线里对height、widht和获取ie下的opacity这几个css属性进行了特殊处理。先分析处理代码最少的。对于ie下的opacity属性，ie并没有opacity这个css属性，而是有自己的filter属性。该值的合法值是浮点值。于是代码里使用了正则表达式移除获取的开头的非数字字符和js下的parseFloat函数将处理后的字符串转化成浮点值。对高度和宽度的获取的处理，是受W3C盒子模型的影响。至于W3C盒子模型的特点可以去参考 Google &nbsp;搜索。由于盒子模型的特点，将padding和border宽度设置为0后我们就可以得到设置的元素的宽度和高度了，而不是盒子模型结合padding和border宽度计算后的宽度和高度。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">var</span> old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];<span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i <span style="line-height:1.5;color:#0000ff;">in</span> d ) {
    old["padding" + d[i]] = 0;
    old["border" + d[i] + "Width"] = 0;
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348044.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">接下来的代码则是判断元素的display属性。如果不是none的话，直接取元素的offsetHeight和offsetWidth值。如果是none的话，再不破坏元素e的属性的前提下，需要将元素e克隆一份，对克隆的这一份应用css属性{visibility:"hidden",position:"absolute",display:"block"}并获取clientHeight和clientWidth值。最后清理现场。注意以下代码中使用的css函数是jQuery对象上的css函数，而不是jQuery本身的css函数。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">e = $(e.cloneNode(<span style="line-height:1.5;color:#0000ff;">true</span>)).css({
    visibility: "hidden", position: "absolute", display: "block"
}).prependTo("body")[0];</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348044.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">追寻css的定义，在jQuery.fn = jQuery.prototype = { ... } 代码中，css实现非常简单，是对attr函数的封装。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">css: <span style="line-height:1.5;color:#0000ff;">function</span>( key, value ) {
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.attr( key, value, "curCSS" );
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348044.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">分析attr函数的实现</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">attr: <span style="line-height:1.5;color:#0000ff;">function</span>( key, value, type ) {
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Check to see if we're setting style values</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">return</span> key.constructor != String || value != undefined ?
        <span style="line-height:1.5;color:#0000ff;">this</span>.each(<span style="line-height:1.5;color:#0000ff;">function</span>(){
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> See if we're setting a hash of styles</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">if</span> ( value == undefined )
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Set all the styles</span><span style="line-height:1.5;color:#008000;"> </span>                <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> prop <span style="line-height:1.5;color:#0000ff;">in</span> key )
                    jQuery.attr(
                        type ? <span style="line-height:1.5;color:#0000ff;">this</span>.style : <span style="line-height:1.5;color:#0000ff;">this</span>,
                        prop, key[prop]
                    );
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> See if we're setting a single key/value style</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">else</span>                 jQuery.attr(
                    type ? <span style="line-height:1.5;color:#0000ff;">this</span>.style : <span style="line-height:1.5;color:#0000ff;">this</span>,
                    key, value
                );
        }) :
         
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Look for the case where we're accessing a style value</span><span style="line-height:1.5;color:#008000;"> </span>        jQuery[ type || "attr" ]( <span style="line-height:1.5;color:#0000ff;">this</span>[0], key );
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348044.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">attr函数的实现里分为两部分设置指定属性值（设置一个属性的写法、设置多个属性的写法），获取指定属性的值。获取属性的值的代码只有一行即</p>
<pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;color:#333333;font-size:14px;line-height:25px;text-align:left;background-color:#ffffff;">jQuery[ type || "attr" ]( this[0], key );</pre><p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">执行这一句的条件是key是字符串(key.constructor == String)并且value 为undefined，这个时候是需要获取指定属性的值。需要注意的是获取css属性值和获取非css属性值是如何实现的。jQuery[type||'attr']起到了区别获取css属性或者非css属性的效果。控制是否获取css属性的参数type有外界传入，上面css函数对attr函数的引用就是一个例子。对于设置指定属性的值，我们可以看到代码里区分为传入了value和没传入value两部分。type依旧作为区分是设置css属性或者非css属性的参数。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这样init函数就算是解析完成了，通过分析可以看出init函数完成对jQuery对象上的功能的拓展。在init同代码段的extend中还有一个函数没有分析，他就是clean函数。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">clean: <span style="line-height:1.5;color:#0000ff;">function</span>(a) {
    <span style="line-height:1.5;color:#0000ff;">var</span> r = [];
    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; a.length; i++ ) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( a[i].constructor == String ) {
            <span style="line-height:1.5;color:#0000ff;">var</span> table = "";
            <span style="line-height:1.5;color:#0000ff;">if</span> ( !a[i].indexOf("&lt;thead") || !a[i].indexOf("&lt;tbody") ) {
                table = "thead";
                a[i] = "&lt;table&gt;" + a[i] + "&lt;/table&gt;";
            } <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( !a[i].indexOf("&lt;tr") ) {
                table = "tr";
                a[i] = "&lt;table&gt;" + a[i] + "&lt;/table&gt;";
            } <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( !a[i].indexOf("&lt;td") || !a[i].indexOf("&lt;th") ) {
                table = "td";
                a[i] = "&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;" + a[i] + "&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;";
            }
            <span style="line-height:1.5;color:#0000ff;">var</span> div = document.createElement("div");
            div.innerHTML = a[i];
            <span style="line-height:1.5;color:#0000ff;">if</span> ( table ) {
                div = div.firstChild;
                <span style="line-height:1.5;color:#0000ff;">if</span> ( table != "thead" ) div = div.firstChild;
                <span style="line-height:1.5;color:#0000ff;">if</span> ( table == "td" ) div = div.firstChild;
            }
            <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> j = 0; j &lt; div.childNodes.length; j++ )
                r.push( div.childNodes[j] );
        }<span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( a[i].jquery || a[i].length &amp;&amp; !a[i].nodeType ){
         
            <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> k = 0; k &lt; a[i].length; k++ )
                r.push( a[i][k] );
        }<span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( a[i] !== <span style="line-height:1.5;color:#0000ff;">null</span> ){
         
            r.push( a[i].nodeType ? a[i] : document.createTextNode(a[i].toString()) );
        }
    }
    <span style="line-height:1.5;color:#0000ff;">return</span> r;
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348044.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">初次看到clean中的代码对table段的处理不明所以，但是结合我们之前提到jQuery可以把html文本处理成jQuery对象的提示就不难理解。clean中有三个if判断。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第一个if判断，对传入的是文本的处理。这段代码里有对不完整的table文本进行处理。比如传入字符串'&lt;thead&gt;&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/thead&gt;' 等。需要组成完整的table描述字符串，否则不能正确解析。接下来就是通过div.innerHTML = a[i]; 将html文本变成DOM元素。在接下来是根据之前处理过程中产生的table变量对产生的DOM元素进行处理。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">if</span> ( table ) {
    div = div.firstChild;
    <span style="line-height:1.5;color:#0000ff;">if</span> ( table != "thead" ) div = div.firstChild;
    <span style="line-height:1.5;color:#0000ff;">if</span> ( table == "td" ) div = div.firstChild;
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348044.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这段代码的逻辑比较精细，需要仔细理解啊。主要是去除完整table中对应传入的html所描述的节点。最后就是将产生的DOM节点加入结果集中。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第二个if判断，如果是jQuery对象或者当前a[i]有length属性并且a[i]不是DOM节点时。将a[i][]数组中的值一个个加入结果集中。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第三个if判断，如果a[i]不为null。如果a[i]是DOM节点则直接加入结果集。如果a[i]不是DOM节点，则把a[i]的字符串表示创建陈一个document.crateTextNode加入结果集</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">最后返回产生的结果集。需要注意，返回额这个结果集是一个DOM节点数组。</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-nine.html]]></link>
<title><![CDATA[（九）jQuery.extend代码段]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 16:04:17 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<div class="wiki-content" style="color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">再次返回看init的代码，我抽离出一个init的简明结构：</p>
<div class="cnblogs_code" style="background-color:#f5f5f5;font-family:'Courier New';font-size:12px;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">init: <span style="line-height:1.5;color:#0000ff;">function</span>(){
    jQuery.initDone = <span style="line-height:1.5;color:#0000ff;">true</span>;
     
    jQuery.each( jQuery.macros.axis, <span style="line-height:1.5;color:#0000ff;">function</span>(i,n){
        jQuery.fn[ i ] = <span style="line-height:1.5;color:#0000ff;">function</span>(a) {
            ...
        };
    });
     
    jQuery.each( jQuery.macros.to, <span style="line-height:1.5;color:#0000ff;">function</span>(i,n){
        jQuery.fn[ i ] = <span style="line-height:1.5;color:#0000ff;">function</span>(){
            ...
        };
    });
     
    jQuery.each( jQuery.macros.each, <span style="line-height:1.5;color:#0000ff;">function</span>(i,n){
        jQuery.fn[ i ] = <span style="line-height:1.5;color:#0000ff;">function</span>() {
            ...
        };
    });
    jQuery.each( jQuery.macros.filter, <span style="line-height:1.5;color:#0000ff;">function</span>(i,n){
        jQuery.fn[ n ] = <span style="line-height:1.5;color:#0000ff;">function</span>(num,fn) {
            ...
        };
    });
     
    jQuery.each( jQuery.macros.attr, <span style="line-height:1.5;color:#0000ff;">function</span>(i,n){
        n = n || i;
        jQuery.fn[ i ] = <span style="line-height:1.5;color:#0000ff;">function</span>(h) {
            ...
        };
    });
    jQuery.each( jQuery.macros.css, <span style="line-height:1.5;color:#0000ff;">function</span>(i,n){
        jQuery.fn[ n ] = <span style="line-height:1.5;color:#0000ff;">function</span>(h) {
            ...
        };
    });
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348034.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">注意每一段each的第二个function参数，该function里定义了jQuery.fn上的属性。也就是说，这里所有的each都是在拓展jQuery对象上的功能。</p>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">先开始从第一个each代码开始看，init函数如何拓展jQuery对象上的功能。发现调用了pushStack，查看pushStack方法发现调用了each 和 get方法。这两个方法均在jQuery.fn = jQuery.prototype =&nbsp;{ ... } 代码段的定义里。</p>
<div class="cnblogs_code" style="background-color:#f5f5f5;font-family:'Courier New';font-size:12px;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">each: <span style="line-height:1.5;color:#0000ff;">function</span>( fn, args ) {
    <span style="line-height:1.5;color:#0000ff;">return</span> jQuery.each( <span style="line-height:1.5;color:#0000ff;">this</span>, fn, args );
}
get: <span style="line-height:1.5;color:#0000ff;">function</span>( num ) {
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Watch for when an array (of elements) is passed in</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( num &amp;&amp; num.constructor == Array ) {
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Use a tricky hack to make the jQuery object</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> look and feel like an array</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">this</span>.length = 0;
        [].push.apply( <span style="line-height:1.5;color:#0000ff;">this</span>, num );
         
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>;
    } <span style="line-height:1.5;color:#0000ff;">else</span>         <span style="line-height:1.5;color:#0000ff;">return</span> num == undefined ?
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Return a 'clean' array</span><span style="line-height:1.5;color:#008000;"> </span>            jQuery.map( <span style="line-height:1.5;color:#0000ff;">this</span>, <span style="line-height:1.5;color:#0000ff;">function</span>(a){ <span style="line-height:1.5;color:#0000ff;">return</span> a } ) :
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Return just the object</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">this</span>[num];
}
pushStack: <span style="line-height:1.5;color:#0000ff;">function</span>(a,args) {
    <span style="line-height:1.5;color:#0000ff;">var</span> fn = args &amp;&amp; args[args.length-1];
    <span style="line-height:1.5;color:#0000ff;">if</span> ( !fn || fn.constructor != Function ) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( !<span style="line-height:1.5;color:#0000ff;">this</span>.stack ) <span style="line-height:1.5;color:#0000ff;">this</span>.stack = [];
        <span style="line-height:1.5;color:#0000ff;">this</span>.stack.push( <span style="line-height:1.5;color:#0000ff;">this</span>.get() );
        <span style="line-height:1.5;color:#0000ff;">this</span>.get( a );
    } <span style="line-height:1.5;color:#0000ff;">else</span> {
        <span style="line-height:1.5;color:#0000ff;">var</span> old = <span style="line-height:1.5;color:#0000ff;">this</span>.get();
        <span style="line-height:1.5;color:#0000ff;">this</span>.get( a );
        <span style="line-height:1.5;color:#0000ff;">if</span> ( fn.constructor == Function )
            <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.each( fn );
        <span style="line-height:1.5;color:#0000ff;">this</span>.get( old );
    }
    <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>;
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348034.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">首先看each函数实现。该each函数仅仅是对jQuery.each的封装，隐含each作用对象为当前的jQuery对象。</p>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">再看get函数的实现。当向get传递的参数num为数组时，则将该数组(作为一个整体，而不是把数组中的值一个个的)入栈到jQuery对象上，并将入栈后的jQuery对象返回。当num参数为非数组时，如果未定义(一般为不传参时)则返回当前jQuery对象所有属性值的数组；如果定义了，则返回当前jQuery对象上此参数作为key的值。</p>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">最后看pushStack函数的实现。这里有一个问题，a参数一般会是什么值？经过研究许久发现，a参数一般会是数组值。所以，讨论当a是数组时pushStack的一般意义。为什么a大部分情况下是数组呢？查看pushStack的大部分引用代码就可以知道了：</p>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">&nbsp;</p>
<div class="cnblogs_code" style="background-color:#f5f5f5;font-family:'Courier New';font-size:12px;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.fn[ i ] = <span style="line-height:1.5;color:#0000ff;">function</span>(a) {
    <span style="line-height:1.5;color:#0000ff;">var</span> ret = jQuery.map(<span style="line-height:1.5;color:#0000ff;">this</span>,n);
    <span style="line-height:1.5;color:#0000ff;">if</span> ( a &amp;&amp; a.constructor == String )
        ret = jQuery.filter(a,ret).r;
    <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.pushStack( ret, arguments );
};</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348034.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">如果fn未设置或者不为函数的话，则pushStack先将当前jQuery对象的DOM元素压入堆栈，然后将新的a压入当前的jQuery对象。最后将这个对象返回。这里需要再次对get函数进行说明。get函数影响了当前的jQuery对象的length属性，所以在目前没有找到明显定义或设置jQuery对象的length属性的代码之前get的调用值得注意。当fn为函数时pushStack将a通过this.get(a)保存到this上，并对this调用each处理，而this.get(old)则不会被执行。于是翻看未分析过的代码，发现最开始jQuery(a,c){ ... } 的代码中调用了get函数。结合jQuery(a,c){ ... } 是创建jQuery对象的主入口和一些以往使用jQuery的经验，摘录实现代码中几行引起我注意的代码：</p>
<div class="cnblogs_code" style="background-color:#f5f5f5;font-family:'Courier New';font-size:12px;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Handle HTML strings</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">var</span> m = /^[^&lt;]*(&lt;.+&gt;)[^&gt;]*$/.exec(a);<span style="line-height:1.5;color:#0000ff;">if</span> ( m ) a = jQuery.clean( [ m[1] ] );<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Watch for when an array is passed in</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">this</span>.get( a.constructor == Array || a.length &amp;&amp; !a.nodeType &amp;&amp; a[0] != undefined &amp;&amp; a[0].nodeType ?
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Assume that it is an array of DOM Elements</span><span style="line-height:1.5;color:#008000;"> </span>    jQuery.merge( a, [] ) :
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Find the matching elements and save them for later</span><span style="line-height:1.5;color:#008000;"> </span>    jQuery.find( a, c ) );</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348034.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">首先，jQuery.length的初始化以及jQuery对象的jQueryObject[index]是什么值，在最后一行比较大的代码里可以得到解读。由于这段代码处在创建jQuery对象的地方，所以创建jQuery对象之后则会因为调用了一次get函数而将length属性初始化，具体初始化的值要根据get( ... ) 中的表达式计算出的值来确定了。这一段表达式中的注释 // Assume that it is an array of DOM Elements 假设a是一个DOM元素的数组 我们可以看出这段表达式返回的是一个DOM元素数组。所以在对jQuery对象进行each遍历时，是遍历的这个DOM元素数组。要记得each中对于有length属性的参数obj是采用数组遍历的形式的。这里，我们也要对find和filter的返回值有一个总结：那就是返回一个DOM元素数组。</p>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">再看最开始两行对jQuery.clean函数的用法，在后面分析clean时需要分析到这两行代码。现在先解析一下传递给clean的是什么参数呢？第一行是一段正则表达式，而m[1]是正则表达式中第一个元组匹配的结果。还记不记得jQuery可以根据传入的一段html文本，并返回jQuery对象。没错，这里的m[1]就是匹配出你传入的html文本这种鬼东西。在接下来的分析clean代码时要记得这点。</p>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">我现在可以理解第一段each代码所实现的内容了：</p>
<div class="cnblogs_code" style="background-color:#f5f5f5;font-family:'Courier New';font-size:12px;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.each( jQuery.macros.axis, <span style="line-height:1.5;color:#0000ff;">function</span>(i,n){
    jQuery.fn[ i ] = <span style="line-height:1.5;color:#0000ff;">function</span>(a) {
        <span style="line-height:1.5;color:#0000ff;">var</span> ret = jQuery.map(<span style="line-height:1.5;color:#0000ff;">this</span>,n);
        <span style="line-height:1.5;color:#0000ff;">if</span> ( a &amp;&amp; a.constructor == String )
            ret = jQuery.filter(a,ret).r;
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.pushStack( ret, arguments );
    };
});</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348034.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">拓展jQuery对象身上的axis类方法，该方法主题过程是：按照方法属性即n指定的操作，获取当前jQuery对象所代表的DOM元素的指定DOM元素并以数组形式返回。如果设定了过滤条件，那么对获取的数组进行过滤。ret仍旧是DOM元素数组。最后将DOM数组pushStack会当前jQuery对象，并将该对象返回。这里需要注意的是，return pushStack(ret,arguments)操作并没有创建新的jQuery对象，而是将当前jQuery对象所对应的DOM元素替换为ret，并将老的DOM元素数组放入堆栈以便调用get方法可以再次取出。</p>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">第二段each代码的实现：</p>
<div class="cnblogs_code" style="background-color:#f5f5f5;font-family:'Courier New';font-size:12px;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.each( jQuery.macros.to, <span style="line-height:1.5;color:#0000ff;">function</span>(i,n){
    jQuery.fn[ i ] = <span style="line-height:1.5;color:#0000ff;">function</span>(){
        <span style="line-height:1.5;color:#0000ff;">var</span> a = arguments;
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.each(<span style="line-height:1.5;color:#0000ff;">function</span>(){
            <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> j = 0; j &lt; a.length; j++ )
                $(a[j])[n]( <span style="line-height:1.5;color:#0000ff;">this</span> );
        });
    };
});</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348034.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">to中是进行的DOM节点插入操作。arguments是指这些操作的参数，可以是节点数组，即传入多个节点。这个each代码是进行的对给定参数中的每个节点调用当前jQuery对象所关联的DOM元素的指定方法。</p>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">第三段each代码的实现</p>
<div class="cnblogs_code" style="background-color:#f5f5f5;font-family:'Courier New';font-size:12px;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.each( jQuery.macros.filter, <span style="line-height:1.5;color:#0000ff;">function</span>(i,n){
    jQuery.fn[ n ] = <span style="line-height:1.5;color:#0000ff;">function</span>(num,fn) {
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.filter( ":" + n + "(" + num + ")", fn );
    };
});</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348034.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">这一段代码是拓展jQuery对象上的过滤类方法。实现很简单，即是调用当前jQuery对象上的filter方法。</p>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">第四段each代码的实现</p>
<div class="cnblogs_code" style="background-color:#f5f5f5;font-family:'Courier New';font-size:12px;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.each( jQuery.macros.attr, <span style="line-height:1.5;color:#0000ff;">function</span>(i,n){
    n = n || i;
    jQuery.fn[ i ] = <span style="line-height:1.5;color:#0000ff;">function</span>(h) {
        <span style="line-height:1.5;color:#0000ff;">return</span> h == undefined ?
            <span style="line-height:1.5;color:#0000ff;">this</span>.length ? <span style="line-height:1.5;color:#0000ff;">this</span>[0][n] : <span style="line-height:1.5;color:#0000ff;">null</span> :
            <span style="line-height:1.5;color:#0000ff;">this</span>.attr( n, h );
    };
});</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348034.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">这一段代码是拓展jQuery对象上的DOM节点属性操作方法。注意，这里在return时实现了指定h时设置指定属性，未指定时获取值的功能。</p>
<div class="cnblogs_code" style="background-color:#f5f5f5;font-family:'Courier New';font-size:12px;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.each( jQuery.macros.css, <span style="line-height:1.5;color:#0000ff;">function</span>(i,n){
    jQuery.fn[ n ] = <span style="line-height:1.5;color:#0000ff;">function</span>(h) {
        <span style="line-height:1.5;color:#0000ff;">return</span> h == undefined ?
            ( <span style="line-height:1.5;color:#0000ff;">this</span>.length ? jQuery.css( <span style="line-height:1.5;color:#0000ff;">this</span>[0], n ) : <span style="line-height:1.5;color:#0000ff;">null</span> ) :
            <span style="line-height:1.5;color:#0000ff;">this</span>.css( n, h );
    };
});</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348034.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这段代码是拓展jQuery对象的css类方法。实现了与属性一样的传入值则设置css属性否则获取css属性值的功能。我们看到jQuery.css 和 jQuery.fn.css（即this.css）我们还没有分析，于是</p>
<br class="Apple-interchange-newline" />]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-eight.html]]></link>
<title><![CDATA[（八）jQuery.extend代码段]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 15:28:38 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">分析jQuery.find的实现代码，发现其调用了 getAll 、trim、sliding三个方法。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">getAll: <span style="line-height:1.5;color:#0000ff;">function</span>(o,r) {
    r = r || [];
    <span style="line-height:1.5;color:#0000ff;">var</span> s = o.childNodes;
    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; s.length; i++ )
        <span style="line-height:1.5;color:#0000ff;">if</span> ( s[i].nodeType == 1 ) {
            r.push( s[i] );
            jQuery.getAll( s[i], r );
        }
    <span style="line-height:1.5;color:#0000ff;">return</span> r;
}
trim: <span style="line-height:1.5;color:#0000ff;">function</span>(t){
    <span style="line-height:1.5;color:#0000ff;">return</span> t.replace(/^\s+|\s+$/g, "");
}
sibling: <span style="line-height:1.5;color:#0000ff;">function</span>(elem, pos, not) {
    <span style="line-height:1.5;color:#0000ff;">var</span> elems = [];
    <span style="line-height:1.5;color:#0000ff;">var</span> siblings = elem.parentNode.childNodes;
    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; siblings.length; i++ ) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( not === <span style="line-height:1.5;color:#0000ff;">true</span> &amp;&amp; siblings[i] == elem ) <span style="line-height:1.5;color:#0000ff;">continue</span>;
        <span style="line-height:1.5;color:#0000ff;">if</span> ( siblings[i].nodeType == 1 )
            elems.push( siblings[i] );
        <span style="line-height:1.5;color:#0000ff;">if</span> ( siblings[i] == elem )
            elems.n = elems.length - 1;
    }
    <span style="line-height:1.5;color:#0000ff;">return</span> jQuery.extend( elems, {
        last: elems.n == elems.length - 1,
        cur: pos == "even" &amp;&amp; elems.n % 2 == 0 || pos == "odd" &amp;&amp; elems.n % 2 || elems[pos] == elem,
        prev: elems[elems.n - 1],
        next: elems[elems.n + 1]
    });
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348013.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">getAll的定义为，将o对象的所有子级节点获取到r集合中。代码内使用了递归调用。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">trim则为通过正则表达式将字符串首尾的连续空白字符移除。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">sibling为获取兄弟节点的意思。not参数表示是否将自己也包含仅结果结合中，为true时则表示不包含，否则包含进结果集合里。注意该函数实现代码里在return时，拓展了结果集合的几个属性。last属性-表示自己是否是兄弟节点中的最后一个。cur属性-表示自己是否出现在指定的位置，位置属性有三种：奇数位置、偶数位置、指定位置。pref属性-表示自己的前一个兄弟节点。next属性-表示自己的后一个兄弟节点。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">接下来我们看find函数的实现代码：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">find: <span style="line-height:1.5;color:#0000ff;">function</span>( t, context ) {
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Make sure that the context is a DOM Element</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( context &amp;&amp; context.nodeType == undefined )
        context = <span style="line-height:1.5;color:#0000ff;">null</span>;
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Set the correct context (if none is provided)</span><span style="line-height:1.5;color:#008000;"> </span>    context = context || jQuery.context || document;
    <span style="line-height:1.5;color:#0000ff;">if</span> ( t.constructor != String ) <span style="line-height:1.5;color:#0000ff;">return</span> [t];
    <span style="line-height:1.5;color:#0000ff;">if</span> ( !t.indexOf("//") ) {
        context = context.documentElement;
        t = t.substr(2,t.length);
    } <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( !t.indexOf("/") ) {
        context = context.documentElement;
        t = t.substr(1,t.length);
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> FIX Assume the root element is right :(</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">if</span> ( t.indexOf("/") &gt;= 1 )
            t = t.substr(t.indexOf("/"),t.length);
    }
    <span style="line-height:1.5;color:#0000ff;">var</span> ret = [context];
    <span style="line-height:1.5;color:#0000ff;">var</span> done = [];
    <span style="line-height:1.5;color:#0000ff;">var</span> last = <span style="line-height:1.5;color:#0000ff;">null</span>;
    <span style="line-height:1.5;color:#0000ff;">while</span> ( t.length &gt; 0 &amp;&amp; last != t ) {
        <span style="line-height:1.5;color:#0000ff;">var</span> r = [];
        last = t;
        t = jQuery.trim(t).replace( /^\/\//i, "" );
         
        <span style="line-height:1.5;color:#0000ff;">var</span> foundToken = <span style="line-height:1.5;color:#0000ff;">false</span>;
         
        <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; jQuery.token.length; i += 2 ) {
            <span style="line-height:1.5;color:#0000ff;">var</span> re = <span style="line-height:1.5;color:#0000ff;">new</span> RegExp("^(" + jQuery.token[i] + ")");
            <span style="line-height:1.5;color:#0000ff;">var</span> m = re.exec(t);
             
            <span style="line-height:1.5;color:#0000ff;">if</span> ( m ) {
                r = ret = jQuery.map( ret, jQuery.token[i+1] );
                t = jQuery.trim( t.replace( re, "" ) );
                foundToken = <span style="line-height:1.5;color:#0000ff;">true</span>;
            }
        }
         
        <span style="line-height:1.5;color:#0000ff;">if</span> ( !foundToken ) {
            <span style="line-height:1.5;color:#0000ff;">if</span> ( !t.indexOf(",") || !t.indexOf("|") ) {
                <span style="line-height:1.5;color:#0000ff;">if</span> ( ret[0] == context ) ret.shift();
                done = jQuery.merge( done, ret );
                r = ret = [context];
                t = " " + t.substr(1,t.length);
            } <span style="line-height:1.5;color:#0000ff;">else</span> {
                <span style="line-height:1.5;color:#0000ff;">var</span> re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
                <span style="line-height:1.5;color:#0000ff;">var</span> m = re2.exec(t);
     
                <span style="line-height:1.5;color:#0000ff;">if</span> ( m[1] == "#" ) {
                    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Ummm, should make this work in all XML docs</span><span style="line-height:1.5;color:#008000;"> </span>                    <span style="line-height:1.5;color:#0000ff;">var</span> oid = document.getElementById(m[2]);
                    r = ret = oid ? [oid] : [];
                    t = t.replace( re2, "" );
                } <span style="line-height:1.5;color:#0000ff;">else</span> {
                    <span style="line-height:1.5;color:#0000ff;">if</span> ( !m[2] || m[1] == "." ) m[2] = "*";
     
                    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; ret.length; i++ )
                        r = jQuery.merge( r,
                            m[2] == "*" ?
                                jQuery.getAll(ret[i]) :
                                ret[i].getElementsByTagName(m[2])
                        );
                }
            }
        }
        <span style="line-height:1.5;color:#0000ff;">if</span> ( t ) {
            <span style="line-height:1.5;color:#0000ff;">var</span> val = jQuery.filter(t,r);
            ret = r = val.r;
            t = jQuery.trim(val.t);
        }
    }
    <span style="line-height:1.5;color:#0000ff;">if</span> ( ret &amp;&amp; ret[0] == context ) ret.shift();
    done = jQuery.merge( done, ret );
    <span style="line-height:1.5;color:#0000ff;">return</span> done;
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348013.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">find函数的代码也比较多，粗略浏览下来注意到查找Token的循环与filter中查找parse的循环类似。find函数中引用到了jQuery.token属性，查看jQuery.token代码如下：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">token: [
    "\\.\\.|/\\.\\.", "a.parentNode",
    "&gt;|/", "jQuery.sibling(a.firstChild)",
    "\\+", "jQuery.sibling(a).next",
    "~", <span style="line-height:1.5;color:#0000ff;">function</span>(a){
        <span style="line-height:1.5;color:#0000ff;">var</span> r = [];
        <span style="line-height:1.5;color:#0000ff;">var</span> s = jQuery.sibling(a);
        <span style="line-height:1.5;color:#0000ff;">if</span> ( s.n &gt; 0 )
            <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = s.n; i &lt; s.length; i++ )
                r.push( s[i] );
        <span style="line-height:1.5;color:#0000ff;">return</span> r;
    }
]</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348013.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">审视token中的值定义，发现token中定义的内容类似XPath的作用。多次阅读find代码之后发更加肯定这一点。按照filter中使用的方法给find划分逻辑结构如下：</p>
<ul style="list-style-position:initial;list-style-image:initial;margin-top:10px;margin-right:10px;margin-bottom:10px;margin-left:45px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">处理find作用的对象和作用条件。</li>
</ul>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">循环开始&lt;直到没有了查找条件&gt;</p>
<ul style="list-style-position:initial;list-style-image:initial;margin-top:10px;margin-right:10px;margin-bottom:10px;margin-left:45px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">获取查询集合，传递给filter根据后续条件进行过滤。</li>
</ul>
<ol style="margin-left:2em;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;"><ol style="margin-left:2em;"><li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">获取token，如果获取到token则获取token指定的集合</li>
<li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">在没有获取到token时，检查是否是条件分隔符','和'|'，如果是则将之前处理的结果保存到最终结果里，并初始画当前集合。检查是否是作用于全局的查找条件（按照id查找，按照标签名查找，所有子节点），获取集合。</li>
</ol>
</li>
</ol>
<ul style="list-style-position:initial;list-style-image:initial;margin-top:10px;margin-right:10px;margin-bottom:10px;margin-left:45px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">在获取的集合上应用过滤条件</li>
</ul>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">循环结束</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">处理find作用对象和作用条件：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Make sure that the context is a DOM Element</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">if</span> ( context &amp;&amp; context.nodeType == undefined )
    context = <span style="line-height:1.5;color:#0000ff;">null</span>;<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Set the correct context (if none is provided)</span><span style="line-height:1.5;color:#008000;"> </span>context = context || jQuery.context || document;<span style="line-height:1.5;color:#0000ff;">if</span> ( t.constructor != String ) <span style="line-height:1.5;color:#0000ff;">return</span> [t];<span style="line-height:1.5;color:#0000ff;">if</span> ( !t.indexOf("//") ) {
    context = context.documentElement;
    t = t.substr(2,t.length);
} <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( !t.indexOf("/") ) {
    context = context.documentElement;
    t = t.substr(1,t.length);
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> FIX Assume the root element is right :(</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( t.indexOf("/") &gt;= 1 )
        t = t.substr(t.indexOf("/"),t.length);
}<span style="line-height:1.5;color:#0000ff;">var</span> ret = [context]; <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;">定义了存储中间结果的变量</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">var</span> done = []; <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 存储最终结果的变量</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">var</span> last = <span style="line-height:1.5;color:#0000ff;">null</span>; <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;">存储上一次处理的条件</span></pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348013.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">如上代码中，前三行代码操作了context参数。主要操作为首先判断传入的context是不是正确的html节点。如果不是的话将context设置为null。接下来对context赋值，这里需要注意jQuery.contex在哪里定义的呢？</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">剩下的代码是对find的作用条件t进行处理。当传入的t不是字符串时则直接返回[t]。当t是字符串时，如果t开头为'//' 或'/' 则将context设置为当前文档的根节点。需要注意代码中注释了 //FIX Assume the root element is right&nbsp;<img class="emoticon emoticon-sad" src="http://www.klvoek.com:8090/s/en_GB/3058/7/_/images/icons/emoticons/sad.png" alt="Sad" data-emoticon-name="sad" style="border-top-width:0px;border-right-width:0px;border-bottom-width:0px;border-left-width:0px;border-style:initial;border-color:initial;border-image:initial;" />的代码，这两句代码起到什么效果呢？</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">紧跟后面是几个内部变量的定义。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">循环开始的条件while ( t.length &gt; 0 &amp;&amp; last != t ) { ... } 。这表示当作用条件为空或者作用条件不能再被识别时结束(注意last的意义)。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">获取查询集合，传递给filter根据后续条件进行过滤。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">获取token。用到正则表达式匹配token。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">var</span> r = [];
last = t;
t = jQuery.trim(t).replace( /^\/\//i, "" );<span style="line-height:1.5;color:#0000ff;">var</span> foundToken = <span style="line-height:1.5;color:#0000ff;">false</span>;<span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; jQuery.token.length; i += 2 ) {
    <span style="line-height:1.5;color:#0000ff;">var</span> re = <span style="line-height:1.5;color:#0000ff;">new</span> RegExp("^(" + jQuery.token[i] + ")");
    <span style="line-height:1.5;color:#0000ff;">var</span> m = re.exec(t);
     
    <span style="line-height:1.5;color:#0000ff;">if</span> ( m ) {
        r = ret = jQuery.map( ret, jQuery.token[i+1] );
        t = jQuery.trim( t.replace( re, "" ) );
        foundToken = <span style="line-height:1.5;color:#0000ff;">true</span>;
    }
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348013.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">如果没有获取到token</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">if</span> ( !foundToken ) {
    <span style="line-height:1.5;color:#0000ff;">if</span> ( !t.indexOf(",") || !t.indexOf("|") ) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( ret[0] == context ) ret.shift();
        done = jQuery.merge( done, ret );
        r = ret = [context];
        t = " " + t.substr(1,t.length);
    } <span style="line-height:1.5;color:#0000ff;">else</span> {
        <span style="line-height:1.5;color:#0000ff;">var</span> re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
        <span style="line-height:1.5;color:#0000ff;">var</span> m = re2.exec(t);
        <span style="line-height:1.5;color:#0000ff;">if</span> ( m[1] == "#" ) {
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Ummm, should make this work in all XML docs</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">var</span> oid = document.getElementById(m[2]);
            r = ret = oid ? [oid] : [];
            t = t.replace( re2, "" );
        } <span style="line-height:1.5;color:#0000ff;">else</span> {
            <span style="line-height:1.5;color:#0000ff;">if</span> ( !m[2] || m[1] == "." ) m[2] = "*";
            <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; ret.length; i++ )
                r = jQuery.merge( r,
                    m[2] == "*" ?
                        jQuery.getAll(ret[i]) :
                        ret[i].getElementsByTagName(m[2])
                );
        }
    }
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348013.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">在获取的集合上应用过滤条件</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">if</span> ( t ) {
    <span style="line-height:1.5;color:#0000ff;">var</span> val = jQuery.filter(t,r);
    ret = r = val.r;
    t = jQuery.trim(val.t);
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2348013.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">在这里需要思考find与filter的关系。我认为，find与filter存在着这样一种关系：find在filter之前被调用。也就是说，find判断filter所要作用的集合，filter在获取的集合上做过滤。</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-seven.html]]></link>
<title><![CDATA[（七）jQuery.extend代码段]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 15:15:49 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">就过之前对grep、map、merge的准备，现在可以入手flter函数了：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">filter: <span style="line-height:1.5;color:#0000ff;">function</span>(t,r,not) {
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Figure out if we're doing regular, or inverse, filtering</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">var</span> g = not !== <span style="line-height:1.5;color:#0000ff;">false</span> ? jQuery.grep :
        <span style="line-height:1.5;color:#0000ff;">function</span>(a,f) {<span style="line-height:1.5;color:#0000ff;">return</span> jQuery.grep(a,f,<span style="line-height:1.5;color:#0000ff;">true</span>);};
     
    <span style="line-height:1.5;color:#0000ff;">while</span> ( t &amp;&amp; /^[a-z[({&lt;*:.#]/i.test(t) ) {
        <span style="line-height:1.5;color:#0000ff;">var</span> p = jQuery.parse;
        <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; p.length; i++ ) {
            <span style="line-height:1.5;color:#0000ff;">var</span> re = <span style="line-height:1.5;color:#0000ff;">new</span> RegExp( "^" + p[i][0]
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Look for a string-like sequence</span><span style="line-height:1.5;color:#008000;"> </span>                .replace( 'S', "([a-z*_-][a-z0-9_-]*)" )
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Look for something (optionally) enclosed with quotes</span><span style="line-height:1.5;color:#008000;"> </span>                .replace( 'Q', " *'?\"?([^'\"]*?)'?\"? *" ), "i" );
            <span style="line-height:1.5;color:#0000ff;">var</span> m = re.exec( t );
            <span style="line-height:1.5;color:#0000ff;">if</span> ( m ) {
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Re-organize the match</span><span style="line-height:1.5;color:#008000;"> </span>                <span style="line-height:1.5;color:#0000ff;">if</span> ( p[i][1] )
                    m = ["", m[1], m[3], m[2], m[4]];
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Remove what we just matched</span><span style="line-height:1.5;color:#008000;"> </span>                t = t.replace( re, "" );
                <span style="line-height:1.5;color:#0000ff;">break</span>;
            }
        }
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> :not() is a special case that can be optomized by</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> keeping it out of the expression list</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">if</span> ( m[1] == ":" &amp;&amp; m[2] == "not" )
            r = jQuery.filter(m[3],r,<span style="line-height:1.5;color:#0000ff;">false</span>).r;
         
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Otherwise, find the expression to execute</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">else</span> {
            <span style="line-height:1.5;color:#0000ff;">var</span> f = jQuery.expr[m[1]];
            <span style="line-height:1.5;color:#0000ff;">if</span> ( f.constructor != String )
                f = jQuery.expr[m[1]][m[2]];
                 
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Build a custom macro to enclose it</span><span style="line-height:1.5;color:#008000;"> </span>            eval("f = function(a,i){" +
                ( m[1] == "@" ? "z=jQuery.attr(a,m[3]);" : "" ) +
                "return " + f + "}");
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Execute it against the current filter</span><span style="line-height:1.5;color:#008000;"> </span>            r = g( r, f );
        }
    }
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Return an array of filtered elements (r)</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> and the modified expression string (t)</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">return</span> { r: r, t: t };
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347998.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">filter函数可以分为三段来看待</p>
<ul style="list-style-position:initial;list-style-image:initial;margin-top:10px;margin-right:10px;margin-bottom:10px;margin-left:45px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">获取正确正则处理函数g（来自于grep，主要是对not选择器进行的调整）</li>
</ul>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">循环开始&lt;直到所有的过滤条件处理完&gt;</p>
<ul style="list-style-position:initial;list-style-image:initial;margin-top:10px;margin-right:10px;margin-bottom:10px;margin-left:45px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">从给定过滤条件字符串中取出一个过滤条件</li>
<li style="background-image:url(http://www.cnblogs.com/Skins/Minyx2_Lite/images/icon_miniarrow.gif);padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:15px;list-style:inherit;background-position:0px 9px;background-repeat:no-repeat no-repeat;">根据过滤条件获取对应的正则表达式，并通过g函数对目标集合进行过滤</li>
</ul>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">循环结束</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">&nbsp;</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">获取正确的正则处理函数g，一般的grep是找出符合条件的结果，当在not筛选时要翻转为找出不符合条件的结果。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Figure out if we're doing regular, or inverse, filtering</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">var</span> g = not !== <span style="line-height:1.5;color:#0000ff;">false</span> ? jQuery.grep :
    <span style="line-height:1.5;color:#0000ff;">function</span>(a,f) {<span style="line-height:1.5;color:#0000ff;">return</span> jQuery.grep(a,f,<span style="line-height:1.5;color:#0000ff;">true</span>);};</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347998.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">循环开始</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">while</span> ( t &amp;&amp; /^[a-z[({&lt;*:.#]/i.test(t) ) {
     ...
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347998.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这里的正则表达式，负责检查字符串 t 的开头是不是符合要求的字符。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">从给定的过滤条件字符串中取出一个过滤条件</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">var</span> p = jQuery.parse;<span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; p.length; i++ ) {
    <span style="line-height:1.5;color:#0000ff;">var</span> re = <span style="line-height:1.5;color:#0000ff;">new</span> RegExp( "^" + p[i][0]
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Look for a string-like sequence</span><span style="line-height:1.5;color:#008000;"> </span>        .replace( 'S', "([a-z*_-][a-z0-9_-]*)" )
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Look for something (optionally) enclosed with quotes</span><span style="line-height:1.5;color:#008000;"> </span>        .replace( 'Q', " *'?\"?([^'\"]*?)'?\"? *" ), "i" );
    <span style="line-height:1.5;color:#0000ff;">var</span> m = re.exec( t );
    <span style="line-height:1.5;color:#0000ff;">if</span> ( m ) {
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Re-organize the match</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">if</span> ( p[i][1] )
            m = ["", m[1], m[3], m[2], m[4]];
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Remove what we just matched</span><span style="line-height:1.5;color:#008000;"> </span>        t = t.replace( re, "" );
        <span style="line-height:1.5;color:#0000ff;">break</span>;
    }
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347998.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这段代码里引用了jQuery.parse属性，我们看一下parse属性的定义代码：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> The regular expressions that power the parsing engine</span><span style="line-height:1.5;color:#008000;"> </span>parse: [
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Match: [@value='test'], [@foo]</span><span style="line-height:1.5;color:#008000;"> </span>    [ "\\[ *(@)S *([!*$^=]*) *Q\\]", 1 ],
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Match: [div], [div p]</span><span style="line-height:1.5;color:#008000;"> </span>    [ "(\\[)Q\\]", 0 ],
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Match: :contains('foo')</span><span style="line-height:1.5;color:#008000;"> </span>    [ "(:)S\\(Q\\)", 0 ],
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Match: :even, :last-chlid</span><span style="line-height:1.5;color:#008000;"> </span>    [ "([:.#]*)S", 0 ]
]</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347998.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这两段代码逻辑并不复杂，正则表达式不怎么好的同学一定要沉住气(^_^我看到正则表达式一对一对的特殊字符也眼晕，坚持住就习惯了)。首先，在第一眼看到parse属性的定义时会觉得很莫名奇妙。如果尝试从正则表达式的角度去对里面的内容进行分析的话，你将会被大Q和大S搞的晕头晕脑。然而，如果将parse属性结合最上面filter中的那段代码来分析的话，很容易就找到重点。从最粗略的代码结构看其，filter中这段代码是一个对parse数组的for循环，在循环体对var re赋值的代码我们很快就找到了大Q和大S的奥秘所在。原来，parse中掺杂了作者自己的伪代码思想，即在这一出的for循环中把parse中的伪代码替换并获得最终的正则表达式。接下来的一行代码就是使用正则表达式对目标字符串进行匹配。剩下的代码则是在匹配成功后，对匹配结果和原字符串的处理。对匹配结果进行的处理是按照parse中设置的标记对结果进行重新排序，综合代码可知只有parse中第一行的正则表达式匹配成功的结果需要重新排序。对原字符串的处理则是在匹配成功后一定会执行的处理过程，即将已经提取出的过滤条件从原字符串中移除。最后一句break的作用是，取出一个过滤条件后就跳出分析过滤条件的循环，进入过滤目标集合的阶段。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">根据过滤条件获取对应的正则表达式，并通过g函数对目标集合进行过滤</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> :not() is a special case that can be optomized by</span><span style="line-height:1.5;color:#008000;"> //</span><span style="line-height:1.5;color:#008000;"> keeping it out of the expression list</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">if</span> ( m[1] == ":" &amp;&amp; m[2] == "not" )
    r = jQuery.filter(m[3],r,<span style="line-height:1.5;color:#0000ff;">false</span>).r;<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Otherwise, find the expression to execute</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">else</span> {
    <span style="line-height:1.5;color:#0000ff;">var</span> f = jQuery.expr[m[1]];
    <span style="line-height:1.5;color:#0000ff;">if</span> ( f.constructor != String )
        f = jQuery.expr[m[1]][m[2]];
         
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Build a custom macro to enclose it</span><span style="line-height:1.5;color:#008000;"> </span>    eval("f = function(a,i){" +
        ( m[1] == "@" ? "z=jQuery.attr(a,m[3]);" : "" ) + 
        "return " + f + "}");
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Execute it against the current filter</span><span style="line-height:1.5;color:#008000;"> </span>    r = g( r, f );
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347998.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这个阶段的第一行代码，是对not过滤条件的特殊处理（相当于，正常情况下filter筛选符合条件的结果，而当遇到not过滤条件是，则发生翻转，筛选出不符合条件的结果。条件是指:not(条件)）。接下来的重点，就是对于jQuery.expr的构造了。这个构造思路在我们理解后显得不是很复杂，但是没有头绪时则会感觉一头雾水。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">expr的设计思路是，将jQuery的所有类型的过滤条件，就均分成四组关键字拼接在一起的，如下表：</p>
<div class="table-wrap" style="color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><table class="confluenceTable" style="margin-top:0px;margin-right:auto;margin-bottom:0px;margin-left:auto;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;"><tbody><tr><th class="confluenceTh" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;"><p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">m1</p>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">过滤条件的类型</p>
</th>
<th class="confluenceTh" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;"><p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">m2</p>
<p style="line-height:1.8;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;">&nbsp;</p>
</th>
<th class="confluenceTh" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">m3</th>
<th class="confluenceTh" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">m4</th>
<th class="confluenceTh" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">示例</th>
</tr>
<tr><td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">'' 标签名称</td>
<td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">标签名称</td>
<td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">——</td>
<td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">——</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">div ； div p</td>
</tr>
<tr><td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">'.' 标签的CSS类名</td>
<td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">CSS类名</td>
<td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">——</td>
<td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">——</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">.red</td>
</tr>
<tr><td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">'#' 标签的id属性</td>
<td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">id属性值</td>
<td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">——</td>
<td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">——</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">#bt</td>
</tr>
<tr><td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">'@' 标签的指定属性</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">属性名称</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">比较条件（可以指定也可以不指定）</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">属性的值（可以指定也可以不指定）</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">@add='yy' ; @blob</td>
</tr>
<tr><td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">':' 按照判定条件</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">判定条件名称</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">判定的值</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">——</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">:gt(10) ； :parent</td>
</tr>
<tr><td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">' [ ' 获取长度属性</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">标签名称</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">——</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">——</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">[div</td>
</tr>
</tbody>
</table>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">于是我们有expr的抽象结构如下</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><div class="line number1 index0 alt2"><code class="plain plain">expr:{</code></div>
<div class="line number2 index1 alt1"><code class="plain spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="plain plain">m1:"m2",</code></div>
<pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">    m1:{
        m2:"m3 m4",
        m2:"m3"
    }
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347998.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">至此filter函数算是完成了。在分析filter的相关代码发现expr的'['过滤器中用到了jQuery.find函数。于是查看jQuery.find的代码定义。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;"></p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;"></p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">我觉得这里的merge 和 map函数也属于 extend 和 each 类型的函数，对jQuery其它功能的实现提供基本支撑，需要记熟到心里面去。</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-six.html]]></link>
<title><![CDATA[（六）jQuery.extend代码段]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 15:07:37 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">现在就基本上可以把each中所有的代码弄明白了。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">each: {
    removeAttr: <span style="line-height:1.5;color:#0000ff;">function</span>( key ) {
        <span style="line-height:1.5;color:#0000ff;">this</span>.removeAttribute( key );
    },
    show: <span style="line-height:1.5;color:#0000ff;">function</span>(){
        <span style="line-height:1.5;color:#0000ff;">this</span>.style.display = <span style="line-height:1.5;color:#0000ff;">this</span>.oldblock ? <span style="line-height:1.5;color:#0000ff;">this</span>.oldblock : "";
        <span style="line-height:1.5;color:#0000ff;">if</span> ( jQuery.css(<span style="line-height:1.5;color:#0000ff;">this</span>,"display") == "none" )
            <span style="line-height:1.5;color:#0000ff;">this</span>.style.display = "block";
    },
    hide: <span style="line-height:1.5;color:#0000ff;">function</span>(){
        <span style="line-height:1.5;color:#0000ff;">this</span>.oldblock = <span style="line-height:1.5;color:#0000ff;">this</span>.oldblock || jQuery.css(<span style="line-height:1.5;color:#0000ff;">this</span>,"display");
        <span style="line-height:1.5;color:#0000ff;">if</span> ( <span style="line-height:1.5;color:#0000ff;">this</span>.oldblock == "none" )
            <span style="line-height:1.5;color:#0000ff;">this</span>.oldblock = "block";
        <span style="line-height:1.5;color:#0000ff;">this</span>.style.display = "none";
    },
    toggle: <span style="line-height:1.5;color:#0000ff;">function</span>(){
        $(<span style="line-height:1.5;color:#0000ff;">this</span>)[ $(<span style="line-height:1.5;color:#0000ff;">this</span>).is(":hidden") ? "show" : "hide" ].apply( $(<span style="line-height:1.5;color:#0000ff;">this</span>), arguments );
    },
    addClass: <span style="line-height:1.5;color:#0000ff;">function</span>(c){
        jQuery.className.add(<span style="line-height:1.5;color:#0000ff;">this</span>,c);
    },
    removeClass: <span style="line-height:1.5;color:#0000ff;">function</span>(c){
        jQuery.className.remove(<span style="line-height:1.5;color:#0000ff;">this</span>,c);
    },
    toggleClass: <span style="line-height:1.5;color:#0000ff;">function</span>( c ){
        jQuery.className[ jQuery.className.has(<span style="line-height:1.5;color:#0000ff;">this</span>,c) ? "remove" : "add" ](<span style="line-height:1.5;color:#0000ff;">this</span>,c);
    },
    remove: <span style="line-height:1.5;color:#0000ff;">function</span>(a){
        <span style="line-height:1.5;color:#0000ff;">if</span> ( !a || jQuery.filter( [<span style="line-height:1.5;color:#0000ff;">this</span>], a ).r )
            <span style="line-height:1.5;color:#0000ff;">this</span>.parentNode.removeChild( <span style="line-height:1.5;color:#0000ff;">this</span> );
    },
    empty: <span style="line-height:1.5;color:#0000ff;">function</span>(){
        <span style="line-height:1.5;color:#0000ff;">while</span> ( <span style="line-height:1.5;color:#0000ff;">this</span>.firstChild )
            <span style="line-height:1.5;color:#0000ff;">this</span>.removeChild( <span style="line-height:1.5;color:#0000ff;">this</span>.firstChild );
    },
    bind: <span style="line-height:1.5;color:#0000ff;">function</span>( type, fn ) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( fn.constructor == String )
            fn = <span style="line-height:1.5;color:#0000ff;">new</span> Function("e", ( !fn.indexOf(".") ? "$(this)" : "return " ) + fn);
        jQuery.event.add( <span style="line-height:1.5;color:#0000ff;">this</span>, type, fn );
    },
    unbind: <span style="line-height:1.5;color:#0000ff;">function</span>( type, fn ) {
        jQuery.event.remove( <span style="line-height:1.5;color:#0000ff;">this</span>, type, fn );
    },
    trigger: <span style="line-height:1.5;color:#0000ff;">function</span>( type, data ) {
        jQuery.event.trigger( type, data, <span style="line-height:1.5;color:#0000ff;">this</span> );
    }
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347991.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">简略浏览each中的属性，发现使我们在jQuery对象上经常调用的方法。那这个each跟jQuery的对象有着什么关系呢？</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第一个函数removeAttr移除指定的属性。谷歌搜索removeAttribute</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第二个函数show\hide，在使用jQuery时最经常使用的显隐元素的方法。动态定义了元素上一个叫做oldblock的属性。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第三个函数toggle，里面用到了is函数，实现代码自动判断使用show还是hidde。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第四个函数addClass\removeClass，使用到了jQuery.className里的方法。增加 和删除指定名称的CSS类。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第五个函数toggleClass，自动判断使用addClass和remveClass。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第六个函数remove，使用到了jQuery的filter函数。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第七个empty函数，这里使用this.firstChild的方法判断是否还有子节点很有意思的。在以后的代码里要考虑可以使用this.frstChild进行一些是否有子节点获取第一个节点的操作。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第八个bind函数，这个是对jQuery.event.add的封装，隐含传入了this。注意当传入的fn是字符串时该函数进行的处理</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第九个unbind函数，这个是对jQuery.event.remove的封装，隐含传入了this。这里请考虑，我们在之前的分析中获知jQuery.event.remove需要根据指定的fn.guid进行移除。那么这里的封装，传入的fn不一定有guid属性啊？分析可知，经过add添加过的fn就会具有guid属性。这也涉及到了var tfn = bfn = fn 这三个变量其实是同一个fn的知识点。我通过如下代码验证此说法：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">function</span> getName(){
    <span style="line-height:1.5;color:#0000ff;">var</span> name = 'i was a function';
    <span style="line-height:1.5;color:#0000ff;">return</span> name;
}<span style="line-height:1.5;color:#0000ff;">function</span> assignGuid(fn){
    fn.guid = 100;
}
assignGuid(getName);
alert(getName.guid); <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 弹窗显示 100</span></pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347991.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">于是在这里，如果你传入的fn没有guid的话，则不会移除任何监听函数。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第十个trigger函数，这个是对jQuery.event.trigger的封装，隐含传入了this。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">但是，each中的这些定义是什么作用呢？是不是正如我们猜想，会将each中的属性定义拓展到jQuery对象上呢？</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">回顾我们之前对jQuery.macros的注解，jQuery.macros中定义了to、attr、axis对象，css、filter数组。注意到这里使用了string.splie(char)的方式创建数组。为了能让我们在理解其它部分代码时更加快速，我们将这几个属性列举一下，并通过其字面意思和设置的值来大概定义他们的作用。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">to: {
    appendTo: "append",
    prependTo: "prepend",
    insertBefore: "before",
    insertAfter: "after"
},
 
css: "width,height,top,left,position,float,overflow,color,background".split(","),
filter: [ "eq", "lt", "gt", "contains" ],
attr: {
    val: "value",
    html: "innerHTML",
    id: <span style="line-height:1.5;color:#0000ff;">null</span>,
    title: <span style="line-height:1.5;color:#0000ff;">null</span>,
    name: <span style="line-height:1.5;color:#0000ff;">null</span>,
    href: <span style="line-height:1.5;color:#0000ff;">null</span>,
    src: <span style="line-height:1.5;color:#0000ff;">null</span>,
    rel: <span style="line-height:1.5;color:#0000ff;">null</span> },
axis: {
    parent: "a.parentNode",
    ancestors: jQuery.parents,
    parents: jQuery.parents,
    next: "jQuery.sibling(a).next",
    prev: "jQuery.sibling(a).prev",
    siblings: jQuery.sibling,
    children: "a.childNodes"
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347991.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">to，定义了DOM节点的操作方法，共有四个。但是单凭to字面意思不能获取更进一步的理解。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">css，使用string.split的方式来创建数组，字符串中是一些css属性。从css字面理解这是一个css属性数组。但是，这里的css属性并不全面，那为什么只有这几个呢？</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">filter，定一个是字符串数组，里面是等于、小于、大于、包含的英文或者缩写。综合filter字面意思，这里应该是一些比较过滤的定义，但这怎么起作用的呢？</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">attr，与之前的css类似，这定义的是html标签上会出现的属性，但是也不全面。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">axis，不认识的同学好去translate.google.com了，坐标轴？通过内部值来看定义的是一些子父关系操作啊。里面又是字符串又是直接书写的形式让人很是费解。但是，要对这里面写的字符串有印象，因为大部分和jQuery有关系。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">好了，这样的话对jQuery.macros的理解就算告一段落了。继续返回去看init的实现。在第一段jQuery.each的代码中我发现它使用了map和filter方法。map和filter方法就定义在init之后，于是去查看这两个方法的实现。先从map开始：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">merge: <span style="line-height:1.5;color:#0000ff;">function</span>(first, second) {
    <span style="line-height:1.5;color:#0000ff;">var</span> result = [];
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Move b over to the new array (this helps to avoid</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> StaticNodeList instances)</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> k = 0; k &lt; first.length; k++ )
        result[k] = first[k];
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Now check for duplicates between a and b and only</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> add the unique items</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; second.length; i++ ) {
        <span style="line-height:1.5;color:#0000ff;">var</span> noCollision = <span style="line-height:1.5;color:#0000ff;">true</span>;
         
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> The collision-checking process</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> j = 0; j &lt; first.length; j++ )
            <span style="line-height:1.5;color:#0000ff;">if</span> ( second[i] == first[j] )
                noCollision = <span style="line-height:1.5;color:#0000ff;">false</span>;
             
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If the item is unique, add it</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">if</span> ( noCollision )
            result.push( second[i] );
    }
    <span style="line-height:1.5;color:#0000ff;">return</span> result;
},
map: <span style="line-height:1.5;color:#0000ff;">function</span>(elems, fn) {
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If a string is passed in for the function, make a function</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> for it (a handy shortcut)</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( fn.constructor == String )
        fn = <span style="line-height:1.5;color:#0000ff;">new</span> Function("a","return " + fn);
     
    <span style="line-height:1.5;color:#0000ff;">var</span> result = [];
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Go through the array, translating each of the items to their</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> new value (or values).</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; elems.length; i++ ) {
        <span style="line-height:1.5;color:#0000ff;">var</span> val = fn(elems[i],i);
        <span style="line-height:1.5;color:#0000ff;">if</span> ( val !== <span style="line-height:1.5;color:#0000ff;">null</span> &amp;&amp; val != undefined ) {
            <span style="line-height:1.5;color:#0000ff;">if</span> ( val.constructor != Array ) val = [val];
            result = jQuery.merge( result, val );
        }
    }
    <span style="line-height:1.5;color:#0000ff;">return</span> result;
},</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347991.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">而map则调用了merge函数，merge函数没有调用其它函数。分析merge为，将第二个数组中的数据唯一的添加到第一个数组中去。map是对于指定的elems集合，对其中每个元素调用fn方法进行处理。然后将产生的结果放在新的数组里并返回。这里再次对fn = new Function("a","return "+fn)进行讨论分析。之前，在对jQuery.macros.each.bind的函数的分析中并没有对此代码有太多分析。现在通过如下代码对此进行分析：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">function</span> myFun(){
    <span style="line-height:1.5;color:#0000ff;">return</span> 'ni hao';
}<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;">假设 fn = 'myFun'</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">var</span> fFun = <span style="line-height:1.5;color:#0000ff;">new</span> Function('a','return myFun');
fFun();<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 返回 function myFun(){return 'ni hao';}</span><span style="line-height:1.5;color:#008000;"> //</span><span style="line-height:1.5;color:#008000;">假设 fn = 'myFun()'</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">var</span> sFun = <span style="line-height:1.5;color:#0000ff;">new</span> Function('b','return myFun()');
sFun();<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 返回 ni hao</span></pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347991.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">通过如上代码，当fn仅仅是函数名时fFun的返回结果为函数 即 return fn。当fn中的函数名之后跟上括号时，返回的为该函数的执行结果即 return fn()。在map中显然该是第二种情形才对，那么传递进来的fn真的有括号吗？呵呵，在这里小小的犯了一下混。没有注意到代码里的</p>
<pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;color:#333333;font-size:14px;line-height:25px;text-align:left;background-color:#ffffff;">var val = fn(elems[i],i);</pre><p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">map 和 merge的作用就分析成这样了。接下来看一下filter的实现。粗略浏览了一下filter的实现代码，发现我坠入了jQuery另一个最核心的实现。发现jQuery中filter用到了大量的正则表达式。最后发现最后一段代码中用到了jQuery.attr方法，该方法也在当前extend中定义。打开查看，我滴娘啊，我能看懂了：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">attr: <span style="line-height:1.5;color:#0000ff;">function</span>(elem, name, value){
    <span style="line-height:1.5;color:#0000ff;">var</span> fix = {
        "for": "htmlFor",
        "class": "className",
        "float": "cssFloat",
        innerHTML: "innerHTML",
        className: "className"
    };
    <span style="line-height:1.5;color:#0000ff;">if</span> ( fix[name] ) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( value != undefined ) elem[fix[name]] = value;
        <span style="line-height:1.5;color:#0000ff;">return</span> elem[fix[name]];
    } <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( elem.getAttribute ) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( value != undefined ) elem.setAttribute( name, value );
        <span style="line-height:1.5;color:#0000ff;">return</span> elem.getAttribute( name, 2 );
    } <span style="line-height:1.5;color:#0000ff;">else</span> {
        name = name.replace(/-([a-z])/ig,<span style="line-height:1.5;color:#0000ff;">function</span>(z,b){<span style="line-height:1.5;color:#0000ff;">return</span> b.toUpperCase();});
        <span style="line-height:1.5;color:#0000ff;">if</span> ( value != undefined ) elem[name] = value;
        <span style="line-height:1.5;color:#0000ff;">return</span> elem[name];
    }
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347991.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">以前再使用jQuery写代码的时候，attr方法可以设置属性值也可以获取属性值，而且类似的方法还有几个。现在终于明白attr的这一点是如何实现的了。注意代码</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">if</span> ( value != undefined ) elem[fix[name]] = value;
        <span style="line-height:1.5;color:#0000ff;">return</span> elem[fix[name]];</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347991.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">下面if的三个判断分别是，第一个判断：是否在需要转义的列表里。第二个判断：元素是否具有getAttribute方法。第三个判断：在没有getAttribute方法是设置和获取属性，这大概又是ie浏览器的兼容代码吧。这里比较有意思的内容也有三点，跟这三个判断相对应。</p>
<ol style="margin-left:2em;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">为什么要由fix呢？</li>
<li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">为什么getAttribute传入了一个参数 '2'？</li>
<li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">第三个判断成立后，对name做了什么？</li>
</ol>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">fix中列举的是elem对象上的属性，getAttribute定义没有第二个参数，name会把background-color变成backgroundColor 。总的来看attr的实现还是比较单纯的。再回头继续看filter实现。filter中引用了jQuery.grep，又巧了grep的实现也在当前的extend代码里。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">grep: <span style="line-height:1.5;color:#0000ff;">function</span>(elems, fn, inv) {
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If a string is passed in for the function, make a function</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> for it (a handy shortcut)</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( fn.constructor == String )
        fn = <span style="line-height:1.5;color:#0000ff;">new</span> Function("a","i","return " + fn);
         
    <span style="line-height:1.5;color:#0000ff;">var</span> result = [];
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Go through the array, only saving the items</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> that pass the validator function</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; elems.length; i++ )
        <span style="line-height:1.5;color:#0000ff;">if</span> ( !inv &amp;&amp; fn(elems[i],i) || inv &amp;&amp; !fn(elems[i],i) )
            result.push( elems[i] );
     
    <span style="line-height:1.5;color:#0000ff;">return</span> result;
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347991.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">经过千锤百炼之后这个函数的实现代码理解没有什么难度了。然而，在if( !inv &amp;&amp; fn(elems[i],i) || inv &amp;&amp; !fn(elems[i],i) )的判断里，inv的取值有什么作用呢？fn是什么函数呢？</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">在参数inv的含义上则让人费解。我在filter中找到了如下代码帮助理解：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">filter:<span style="line-height:1.5;color:#0000ff;">function</span>(t,r,not){
    ...
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Figure out if we're doing regular, or inverse, filtering</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">var</span> g = not !== <span style="line-height:1.5;color:#0000ff;">false</span> ? jQuery.grep :
            <span style="line-height:1.5;color:#0000ff;">function</span>(a,f) {<span style="line-height:1.5;color:#0000ff;">return</span> jQuery.grep(a,f,<span style="line-height:1.5;color:#0000ff;">true</span>);};
    ...
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> :not() is a special case that can be optomized by</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> keeping it out of the expression list</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( m[1] == ":" &amp;&amp; m[2] == "not" )
        r = jQuery.filter(m[3],r,<span style="line-height:1.5;color:#0000ff;">false</span>).r;
    ...
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347991.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第一行代码的注释里提到，检查我们是不是依照正则表达是进行筛选。在filter传入not为false时才会使g = function(a,f){return jQuery.grep(a,f,true);}。结合上面第二段代码对jQuery.filter的调用情景，可以得出一般情况下grep返回正则匹配的结果，只有当grep传入的inv=true时(jQuery.filter 的not参数为false时，此时filter中有‘:not’过滤选项)返回的是正则不匹配的结果。而grep中的fn函数则是具有正则表达式函数。</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-five.html]]></link>
<title><![CDATA[（五）jQuery.extend 代码段]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 15:00:52 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">继续分析event中剩余的remove、trigger方法，并附带完全理解handle方法。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Detach an event or set of events from an element</span><span style="line-height:1.5;color:#008000;"> </span>remove: <span style="line-height:1.5;color:#0000ff;">function</span>(element, type, handler) {
    <span style="line-height:1.5;color:#0000ff;">if</span> (element.events)
        <span style="line-height:1.5;color:#0000ff;">if</span> (type &amp;&amp; element.events[type])
            <span style="line-height:1.5;color:#0000ff;">if</span> ( handler )
                <span style="line-height:1.5;color:#0000ff;">delete</span> element.events[type][handler.guid];
            <span style="line-height:1.5;color:#0000ff;">else</span>                 <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i <span style="line-height:1.5;color:#0000ff;">in</span> element.events[type] )
                    <span style="line-height:1.5;color:#0000ff;">delete</span> element.events[type][i];
        <span style="line-height:1.5;color:#0000ff;">else</span>             <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> j <span style="line-height:1.5;color:#0000ff;">in</span> element.events )
                <span style="line-height:1.5;color:#0000ff;">this</span>.remove( element, j );
},
trigger: <span style="line-height:1.5;color:#0000ff;">function</span>(type,data,element) {
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Touch up the incoming data</span><span style="line-height:1.5;color:#008000;"> </span>    data = data || [];
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Handle a global trigger</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( !element ) {
        <span style="line-height:1.5;color:#0000ff;">var</span> g = <span style="line-height:1.5;color:#0000ff;">this</span>.global[type];
        <span style="line-height:1.5;color:#0000ff;">if</span> ( g )
            <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; g.length; i++ )
                <span style="line-height:1.5;color:#0000ff;">this</span>.trigger( type, data, g[i] );
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Handle triggering a single element</span><span style="line-height:1.5;color:#008000;"> </span>    } <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( element["on" + type] ) {
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Pass along a fake event</span><span style="line-height:1.5;color:#008000;"> </span>        data.unshift( <span style="line-height:1.5;color:#0000ff;">this</span>.fix({ type: type, target: element }) );
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Trigger the event</span><span style="line-height:1.5;color:#008000;"> </span>        element["on" + type].apply( element, data );
    }
},
handle: <span style="line-height:1.5;color:#0000ff;">function</span>(event) {
    <span style="line-height:1.5;color:#0000ff;">if</span> ( <span style="line-height:1.5;color:#0000ff;">typeof</span> jQuery == "undefined" ) <span style="line-height:1.5;color:#0000ff;">return</span>;
    event = event || jQuery.event.fix( window.event );
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If no correct event was found, fail</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( !event ) <span style="line-height:1.5;color:#0000ff;">return</span>;
    <span style="line-height:1.5;color:#0000ff;">var</span> returnValue = <span style="line-height:1.5;color:#0000ff;">true</span>;
    <span style="line-height:1.5;color:#0000ff;">var</span> c = <span style="line-height:1.5;color:#0000ff;">this</span>.events[event.type];
    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> j <span style="line-height:1.5;color:#0000ff;">in</span> c ) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( c[j].apply( <span style="line-height:1.5;color:#0000ff;">this</span>, [event] ) === <span style="line-height:1.5;color:#0000ff;">false</span> ) {
            event.preventDefault();
            event.stopPropagation();
            returnValue = <span style="line-height:1.5;color:#0000ff;">false</span>;
        }
    }
     
    <span style="line-height:1.5;color:#0000ff;">return</span> returnValue;
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347976.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">基于之前对events属性和for .. in 的分析，这里handle已经基本上被理解了。handle方法通过判断DOM events的类型执行jQuery对象上为其添加的响应方法。但是这里需要注意returnValue的作用。不要和handle后面fix中的returnValue混淆。这里只是一个普通意义的函数内部变量而已。如果，事件的监听方法列表里有任何一个的返回值为false的话handle就会返回false，继而配合前面提到元素的事件处理被jQuery通过hadle方法接管导致该事件不会再继续被接下来的监听方法处理。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">然后看第一个remove函数的实现。如果对象上的events不存在的话则不做任何操作。如果不传递type或者传递的type在events上不存在时，remove会移除events中所有事件。如果指定的type存在，但是没有指定handler或者指定的handler对应的guid不存在，则直接移除type下所有的监听方法。type 和 handler正确指定时，则移除指定的handler方法。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">trigger函数的实现代码能够回答了我们之前在add中对global属性的疑问。当没有指定element元素时则是对global中所有监听了指定类型事件的元素上触发该事件。这就是为什么add中会把element元素push到global中去了的原因。如果指定了element元素这个参数的话，则会判断指定元素上的对应的on事件是否存在，存在则触发此事件，否则不做任何操作。trigger函数里的element["on"+type].apply(arg)是调用的谁呢？没错，我们可以在add函数的实现代码中找到答案，就是jQuery.event.handle。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">现在我们可以对event:{ ... }中的代码做一个小结了：</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">通过分析add的实现代码，我们就可以看到jQuery的事件处理机制。将元素上指定的事件屏蔽，并定义对应的on-name事件，比如 hover-&gt; onhover。然后将监听方法加入该事件的监听列表里。当调用trigger时则会触发指定的ontype事件。所以，js原事件里是没有on什么的，这是jQuery增加的。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;"><img src="/Upload/EditorImage/image/klvoek/201202/20120212150029_5629.jpg" title="jQuery event" alt="jQuery event" border="0" /></p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jQuery-1pointzero-code-four.html]]></link>
<title><![CDATA[（四）第一个jQuery.extend代码段]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 14:56:44 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第一个jQuery.Extend代码中的init实现较为复杂，尝试分析init引用的函数并进行阅读。代码量有点大，于是点击所有的jQuery.Extend代码查看，发现在第二个new function之上的jQuery.Extend拓展了jQuery的ready方法，该代码段的实现比较简明易读。此段代码是jQuery.extend({..})，extend只有一个参数且为对象时，对象上的属性被拓展到了调用者jQuery身上。于是可以通过jQuery.ready等访问代码中的方法和属性。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.extend({
    <span style="line-height:1.5;color:#008000;">/*</span><span style="line-height:1.5;color:#008000;">      * All the code that makes DOM Ready work nicely.
     </span><span style="line-height:1.5;color:#008000;">*/</span>     isReady: <span style="line-height:1.5;color:#0000ff;">false</span>,
    readyList: [],
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Handle when the DOM is ready</span><span style="line-height:1.5;color:#008000;"> </span>    ready: <span style="line-height:1.5;color:#0000ff;">function</span>() {
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Make sure that the DOM is not already loaded</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">if</span> ( !jQuery.isReady ) {
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Remember that the DOM is ready</span><span style="line-height:1.5;color:#008000;"> </span>            jQuery.isReady = <span style="line-height:1.5;color:#0000ff;">true</span>;
             
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If there are functions bound, to execute</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">if</span> ( jQuery.readyList ) {
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Execute all of them</span><span style="line-height:1.5;color:#008000;"> </span>                <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; jQuery.readyList.length; i++ )
                    jQuery.readyList[i].apply( document );
                 
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Reset the list of functions</span><span style="line-height:1.5;color:#008000;"> </span>                jQuery.readyList = <span style="line-height:1.5;color:#0000ff;">null</span>;
            }
        }
    }
});</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347968.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">上面的代码是一段很简明的代码。稍微阅读几遍就应该知道了这段代码所实现的功能。这段代码实现了jQuery上的ready方法，看到isReady以及其在ready方法中的使用，肯定了我前面对ready只执行一次的假设。我记得jQuery允许多次添加对ready的监听，这里对readyList:[]的定义可以看到实现的基础。但是，这里并没有向readyList中挂载函数的代码，需要在其他地方查找。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">我们再次对第一段jQuery.extend中的init方法进行解读。init方法引用了each，查看each方法实现比较单纯，没有引用依赖其它的方法。each代码如下：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">each: <span style="line-height:1.5;color:#0000ff;">function</span>( obj, fn, args ) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( obj.length == undefined )
            <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i <span style="line-height:1.5;color:#0000ff;">in</span> obj )
                fn.apply( obj[i], args || [i, obj[i]] );
        <span style="line-height:1.5;color:#0000ff;">else</span>             <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; obj.length; i++ )
                fn.apply( obj[i], args || [i, obj[i]] );
        <span style="line-height:1.5;color:#0000ff;">return</span> obj;
    }</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347968.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这里需要注意apply 与 call 的异同。fn.apply(thisObj,[]) &nbsp;fn.call(thisObj,arg1,arg2...)。你可以在网上搜寻apply的作用。这里的重点是，如果obj没有length属性则遍历obj的属性；如果obj有length属性，则把obj当作数组遍历。这里each实现的功能是，对于给定的obj对象进行遍历，并调用fn回调函数，fn中的this指针指向当前for循环比例的obj[key]的值。并向fn传递指定的参数arg或者each默认的参数[key,obj[key]]。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">继续查看init函数实现，发现其中引用了jQuery.macros ，于是去查看jQuery.macros的定义，jQuery.macros中前半部分多时数组属性的定义，这些属性的作用不甚明了。jQuery.macros.each的定义中使用到了jQuery.className。jQuery.className定义在init函数所在的jQuery.extend代码中。返回去查看jQuery.className的定义。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">className: {
        add: <span style="line-height:1.5;color:#0000ff;">function</span>(o,c){
            <span style="line-height:1.5;color:#0000ff;">if</span> (jQuery.className.has(o,c)) <span style="line-height:1.5;color:#0000ff;">return</span>;
            o.className += ( o.className ? " " : "" ) + c;
        },
        remove: <span style="line-height:1.5;color:#0000ff;">function</span>(o,c){
            o.className = !c ? "" :
                o.className.replace(
                    <span style="line-height:1.5;color:#0000ff;">new</span> RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
        },
        has: <span style="line-height:1.5;color:#0000ff;">function</span>(e,a) {
            <span style="line-height:1.5;color:#0000ff;">if</span> ( e.className != undefined )
                e = e.className;
            <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">new</span> RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
        }
    }</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347968.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">先看remove的定义，当不设置c参数时，直接将o对象的className属性置空。当设置了c参数时，通过正则表达式进行删除。has函数，对一个e对象(应该是元素element的缩写)，检查指定的css类名a是否存在。为什么has中使用的正则表达式和remove中使用的正则表达式不同呢？add函数则将指定的类名添加到对象上。如果对象上已经存在类名则不重复添加。jQuery对CSS的草组包含修改CSS属性值和CSS类名。从这里的定义可以看出，作者把对CSS类名的操作通过className组织在了一起。原来如此啊，jQuery对CSS类名的操作是通过正则表达式啊。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">返回继续查看jQuery.macros.each的代码实现，发现其中引用了jQuery.event。jQuery.event的定义也在init所在的jQuery.extend代码段中。event的简明结构如下：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">event: {
     
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Bind an event to an element</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Original by Dean Edwards</span><span style="line-height:1.5;color:#008000;"> </span>        add: <span style="line-height:1.5;color:#0000ff;">function</span>(element, type, handler) {
            ...
        },
         
        guid: 1,
        global: {},
         
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Detach an event or set of events from an element</span><span style="line-height:1.5;color:#008000;"> </span>        remove: <span style="line-height:1.5;color:#0000ff;">function</span>(element, type, handler) {
            ...
        },
         
        trigger: <span style="line-height:1.5;color:#0000ff;">function</span>(type,data,element) {
            ...
        },
         
        handle: <span style="line-height:1.5;color:#0000ff;">function</span>(event) {
            ...
        },
         
        fix: <span style="line-height:1.5;color:#0000ff;">function</span>(event) {
            ...
        }
     
    }</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347968.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">event定义不是很复杂，至少从这个简明结构图上来说。先从add函数顺序向下看，发现add函数代码挺多。于是梳理函数调用关系，fix属于被调用者。于是先从fix函数开始下手：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">fix: <span style="line-height:1.5;color:#0000ff;">function</span>(event) {
    <span style="line-height:1.5;color:#0000ff;">if</span> ( event ) {
        event.preventDefault = <span style="line-height:1.5;color:#0000ff;">function</span>() {
            <span style="line-height:1.5;color:#0000ff;">this</span>.returnValue = <span style="line-height:1.5;color:#0000ff;">false</span>;
        };
     
        event.stopPropagation = <span style="line-height:1.5;color:#0000ff;">function</span>() {
            <span style="line-height:1.5;color:#0000ff;">this</span>.cancelBubble = <span style="line-height:1.5;color:#0000ff;">true</span>;
        };
    }
     
    <span style="line-height:1.5;color:#0000ff;">return</span> event;
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347968.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">上面代码行数很少。但是除看之下有两个疑问：</p>
<ol style="margin-left:2em;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">event.preventDefault 和 event.stopPropagation是否已经定义存在？</li>
<li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">this.returnValue 和 this.cancelBubble 是什么意思？</li>
</ol>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">单独看fix函数的实现很难对这几段代码的作用和意义进行理解。但是我们注意到each.hadle的函数里引用了fix函数，引用代码如下：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">handle: <span style="line-height:1.5;color:#0000ff;">function</span>(event) {
    ...
    event = event || jQuery.event.fix( window.event );
    ...
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347968.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">如上代码的意思是，如果没有传入event的话，则使用经过fix处理的window.event。这究竟是为啥呢？也许对此有所了解的人会很快就明白这是ie下事件处理的一些区别导致的。但是，一开始我明显不属于那群知道的人，所以我选择了谷歌这个问题(Google:window.event)。非常幸运我找到了如下一篇文章：<a class="external-link" href="http://www.quirksmode.org/js/events_access.html" rel="nofollow" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;border-bottom-width:1px;border-bottom-style:dashed;">http://www.quirksmode.org/js/events_access.html</a></p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;"><a class="external-link" href="http://www.quirksmode.org/js/events_access.html" rel="nofollow" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;border-bottom-width:1px;border-bottom-style:dashed;">提炼文章中提到的对于我们理解这个问题的要点如下：</a></p>
<div class="code panel" style="color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;">&nbsp;</div>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> W3C/Netscape</span><span style="line-height:1.5;color:#008000;"> </span>element.onclick = <span style="line-height:1.5;color:#0000ff;">function</span> (e) {alert('Event type is ' + e.type)}<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Microsoft</span><span style="line-height:1.5;color:#008000;"> </span>element.onclick = <span style="line-height:1.5;color:#0000ff;">function</span> () {alert('Event type is ' + window.event.type)}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347968.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;"><span style="line-height:1.8;color:#ccffff;font-size:12px;font-family:楷体;">至于你们看不看得懂，我反正是懂了 - by &nbsp;klvoek</span><br />
原来，ie下的事件响应并不会把event当作参数传递给处理函数，而是需要通过window.event处理啊。如果你想了解更多，欢迎把上面那篇文章读透或者继续使用谷歌搜索。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">那好，handle中的那段代码也是对ie的一段兼容代码啊。接下来fix中的内容就比较好把握了，抱着ie缺、ie缺的心态看如下代码：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">var</span> evtObj;<span style="line-height:1.5;color:#0000ff;">if</span> (document.createEvent) {
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> ("FF");</span><span style="line-height:1.5;color:#008000;"> </span>    evtObj = document.createEvent('MouseEvents');
    evtObj.initEvent('MouseEvents', <span style="line-height:1.5;color:#0000ff;">true</span>, <span style="line-height:1.5;color:#0000ff;">false</span>);
}<span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> (document.createEventObject) {
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;">  ("IE");</span><span style="line-height:1.5;color:#008000;"> </span>    evtObj = document.createEventObject();
}
alert(evtObj); <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 弹窗显示 event object</span><span style="line-height:1.5;color:#008000;"> </span>alert(evtObj.preventDefault); <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 弹窗显示 ie下为undefined W3C兼容浏览器为 native code</span><span style="line-height:1.5;color:#008000;"> </span>alert(evtObj.stopPropagation); <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 弹窗显示 ie下为undefined W3C兼容浏览器为 native code</span></pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347968.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">你需要尝试在ie下和非ie下测试如上代码。这样，刚刚提到的两个问题有了答案：</p>
<ol style="margin-left:2em;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">event.preventDefault 和 event.stopPropagation之前是否已经定义存在？</li>
</ol>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这两个为W3C中定义的事件所属的方法。第一个是阻止调用默认事件的发生，第二个是阻止事件继续触发。</p>
<ol style="margin-left:2em;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">this.returnValue 和 this.cancelBubble 是什么意思？</li>
</ol>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第一个是ie下event的属性，可以用来取消发生的event事件。发散一下以前用来阻止默认事件响应函数执行的代码，到底那个好呢？再有可能需要对浏览器页面上的事件模型有所了解。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">&lt;a click="return false;"&gt;No Response&lt;/a&gt;
  
&lt;a click="return noResponse();"&gt;No Response&lt;/a&gt;<span style="line-height:1.5;color:#0000ff;">function</span> noResponse(){
    <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">false</span>;
 }</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347968.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">cancelBubble是ie下对冒泡事件机制的控制，设置为false则事件不再进行后续冒泡处理。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">&nbsp;再返回去看handle函数的实现</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">handle: <span style="line-height:1.5;color:#0000ff;">function</span>(event) {
    <span style="line-height:1.5;color:#0000ff;">if</span> ( <span style="line-height:1.5;color:#0000ff;">typeof</span> jQuery == "undefined" ) <span style="line-height:1.5;color:#0000ff;">return</span>;
    event = event || jQuery.event.fix( window.event );
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If no correct event was found, fail</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( !event ) <span style="line-height:1.5;color:#0000ff;">return</span>;
    <span style="line-height:1.5;color:#0000ff;">var</span> returnValue = <span style="line-height:1.5;color:#0000ff;">true</span>;
    <span style="line-height:1.5;color:#0000ff;">var</span> c = <span style="line-height:1.5;color:#0000ff;">this</span>.events[event.type];
    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> j <span style="line-height:1.5;color:#0000ff;">in</span> c ) {
        <span style="line-height:1.5;color:#0000ff;">if</span> ( c[j].apply( <span style="line-height:1.5;color:#0000ff;">this</span>, [event] ) === <span style="line-height:1.5;color:#0000ff;">false</span> ) {
            event.preventDefault();
            event.stopPropagation();
            returnValue = <span style="line-height:1.5;color:#0000ff;">false</span>;
        }
    }
     
    <span style="line-height:1.5;color:#0000ff;">return</span> returnValue;
}</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347968.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">看到handle实现中的第一行代码，我的第一感觉是：jQuery不是已经定义了嘛？这样的校验是不是显得多余呢？后来，我回忆起了一段信息。在jQuery中，将所有附加的事件可能在jQuery被销毁的时候没有从元素上解绑定。于是，这一段原本看似冗余和多此一举的代码就不难理解了。如果jQuery被销毁而监听事件没有解绑定的话，后续代码没有继续执行的必要或者继续执行会引发不可预料(作者是认为为不可预料，但是作为看代码的我应该要了解大概会是什么错误。现在还不能给出准确估计，要等继续看过代码之后才能再次评估了)的错误。继续向后看代码，</p>
<ol style="margin-left:2em;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">this.events是什么啊？</li>
<li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">这里貌似没有定义啊。for..in循环，遍历了什么？</li>
</ol>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">里面的内容大概是调用c[j]如果返回了false的话就让当前事件不再继续触发下去。有碍于this.events的定义并没有在event{}的范围内找到，所以考虑handle调用时this指针可能通过apply的形式定位到其它对象上。先对handle函数存疑，返回去从头看add函数的实现。</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-three.html]]></link>
<title><![CDATA[（三）三段new function的分析]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 14:47:10 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">现在我知道new function(){}能够干嘛了。接下来看一下代码中的三个new function(){ ... } 都做了神马？</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">1.第一个，判断浏览器型号和浏览器使用的CSS盒子模型是W3C盒子模型还是IE的Quirks模式</p>
<div class="code panel" style="color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><div class="codeContent panelContent"><div><div id="highlighter_334303" class="syntaxhighlighter nogutter javascript" style="width:1098px;margin-top:1em !important;margin-right:0px !important;margin-bottom:1em !important;margin-left:0px !important;position:relative !important;overflow-x:auto !important;overflow-y:auto !important;font-size:1em !important;"><div class="cnblogs_code" style="background-color:initial !important;font-family:'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important;font-size:12px !important;border-top-width:0px !important;border-right-width:0px !important;border-bottom-width:0px !important;border-left-width:0px !important;border-image:initial !important;padding-top:0px !important;padding-right:0px !important;padding-bottom:0px !important;padding-left:0px !important;word-break:break-all;overflow-x:visible !important;overflow-y:visible !important;margin-top:0px !important;margin-right:0px !important;margin-bottom:0px !important;margin-left:0px !important;border-top-left-radius:0px !important;border-top-right-radius:0px !important;border-bottom-right-radius:0px !important;border-bottom-left-radius:0px !important;background-image:none !important;background-attachment:initial !important;background-origin:initial !important;background-clip:initial !important;border-style:initial !important;border-color:initial !important;bottom:auto !important;float:none !important;height:auto !important;left:auto !important;line-height:2em !important;outline-width:0px !important;outline-style:initial !important;outline-color:initial !important;position:static !important;right:auto !important;top:auto !important;vertical-align:baseline !important;width:auto !important;box-sizing:content-box !important;min-height:inherit !important;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">new</span> <span style="line-height:1.5;color:#0000ff;">function</span>() {
    <span style="line-height:1.5;color:#0000ff;">var</span> b = navigator.userAgent.toLowerCase();
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Figure out what browser is being used</span><span style="line-height:1.5;color:#008000;"> </span>    jQuery.browser = {
        safari: /webkit/.test(b),
        opera: /opera/.test(b),
        msie: /msie/.test(b) &amp;&amp; !/opera/.test(b),
        mozilla: /mozilla/.test(b) &amp;&amp; !/compatible/.test(b)
    };
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Check to see if the W3C box model is being used</span><span style="line-height:1.5;color:#008000;"> </span>    jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
};</pre><div class="cnblogs_code_toolbar" style="margin-top:0px !important;border-top-left-radius:0px !important;border-top-right-radius:0px !important;border-bottom-right-radius:0px !important;border-bottom-left-radius:0px !important;background-image:none !important;background-attachment:initial !important;background-origin:initial !important;background-clip:initial !important;background-color:initial !important;border-top-width:0px !important;border-right-width:0px !important;border-bottom-width:0px !important;border-left-width:0px !important;border-style:initial !important;border-color:initial !important;border-image:initial !important;bottom:auto !important;float:none !important;height:auto !important;left:auto !important;line-height:2em !important;margin-right:0px !important;margin-bottom:0px !important;margin-left:0px !important;outline-width:0px !important;outline-style:initial !important;outline-color:initial !important;overflow-x:visible !important;overflow-y:visible !important;padding-top:0px !important;padding-right:0px !important;padding-bottom:0px !important;padding-left:0px !important;position:static !important;right:auto !important;top:auto !important;vertical-align:baseline !important;width:auto !important;box-sizing:content-box !important;min-height:inherit !important;"><span class="cnblogs_code_copy" style="font-family:'Courier New';padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347961.html" style="outline-style:initial !important;outline-width:0px !important;outline-color:initial !important;text-decoration:none;color:#3d81ee;border-top-left-radius:0px !important;border-top-right-radius:0px !important;border-bottom-right-radius:0px !important;border-bottom-left-radius:0px !important;background-image:none !important;background-attachment:initial !important;background-origin:initial !important;background-clip:initial !important;background-color:initial !important;border-top-width:0px !important;border-right-width:0px !important;border-bottom-width:0px !important;border-left-width:0px !important;border-style:initial !important;border-color:initial !important;border-image:initial !important;bottom:auto !important;float:none !important;height:auto !important;left:auto !important;line-height:2em !important;margin-top:0px !important;margin-right:0px !important;margin-bottom:0px !important;margin-left:0px !important;overflow-x:visible !important;overflow-y:visible !important;padding-top:0px !important;padding-right:0px !important;padding-bottom:0px !important;padding-left:0px !important;position:static !important;right:auto !important;top:auto !important;vertical-align:baseline !important;width:auto !important;box-sizing:content-box !important;font-family:'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace !important;min-height:inherit !important;">复制代码</a></span></div>
</div>
<span style="font-family:monospace;font-size:xx-small;"><span style="line-height:normal;"><br />
</span></span></div>
</div>
</div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第一个好啊，他简单，就做了浏览器型号判断以及浏览器盒子模型的判断。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">首先使用正则表达式判断navigator.userAgent.toLowerCase()字符串，以获取当前的浏览器型号。接下来是判断当前浏览器的W3C盒子模型，当然这都是因为ie有两种模式。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">document.compatMode 这个是ie浏览器中存在的属性。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">我们都知道，IE对盒模型的渲染在 Standards Mode和Quirks Mode是有很大差别的，在Standards Mode下对于盒模型的解释和其他的标准浏览器是一样，但在Quirks Mode模式下则有很大差别，而在不声明Doctype的情况下，IE默认又是Quirks Mode。所以为兼容性考虑，我们可能需要获取当前的文档渲染方式。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">document.compatMode正好派上用场，它有两种可能的返回值：BackCompat和CSS1Compat，对其解释如下：</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">BackCompat Standards-compliant mode is not switched on. (Quirks Mode)</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">CSS1Compat Standards-compliant mode is switched on. (Standards Mode)</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">在实际的项目中，我们还需要在获取浏览是否IE，这样就可以通过判断document.compatMode的值得到IE的渲染模式了。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">&nbsp;</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第二个：jQuery.ready的监听与响应</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">new</span> <span style="line-height:1.5;color:#0000ff;">function</span>(){
    <span style="line-height:1.5;color:#0000ff;">var</span> e = ("blur,focus,load,resize,scroll,unload,click,dblclick," +
        "mousedown,mouseup,mousemove,mouseover,mouseout,change,reset,select," +
        "submit,keydown,keypress,keyup,error").split(",");
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Go through all the event names, but make sure that</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> it is enclosed properly</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; e.length; i++ ) <span style="line-height:1.5;color:#0000ff;">new</span> <span style="line-height:1.5;color:#0000ff;">function</span>(){
             
        <span style="line-height:1.5;color:#0000ff;">var</span> o = e[i];
         
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Handle event binding</span><span style="line-height:1.5;color:#008000;"> </span>        jQuery.fn[o] = <span style="line-height:1.5;color:#0000ff;">function</span>(f){
            <span style="line-height:1.5;color:#0000ff;">return</span> f ? <span style="line-height:1.5;color:#0000ff;">this</span>.bind(o, f) : <span style="line-height:1.5;color:#0000ff;">this</span>.trigger(o);
        };
         
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Handle event unbinding</span><span style="line-height:1.5;color:#008000;"> </span>        jQuery.fn["un"+o] = <span style="line-height:1.5;color:#0000ff;">function</span>(f){ <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.unbind(o, f); };
         
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Finally, handle events that only fire once</span><span style="line-height:1.5;color:#008000;"> </span>        jQuery.fn["one"+o] = <span style="line-height:1.5;color:#0000ff;">function</span>(f){
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Attach the event listener</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.each(<span style="line-height:1.5;color:#0000ff;">function</span>(){
                <span style="line-height:1.5;color:#0000ff;">var</span> count = 0;
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Add the event</span><span style="line-height:1.5;color:#008000;"> </span>                jQuery.event.add( <span style="line-height:1.5;color:#0000ff;">this</span>, o, <span style="line-height:1.5;color:#0000ff;">function</span>(e){
                    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If this function has already been executed, stop</span><span style="line-height:1.5;color:#008000;"> </span>                    <span style="line-height:1.5;color:#0000ff;">if</span> ( count++ ) <span style="line-height:1.5;color:#0000ff;">return</span>;
                 
                    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> And execute the bound function</span><span style="line-height:1.5;color:#008000;"> </span>                    <span style="line-height:1.5;color:#0000ff;">return</span> f.apply(<span style="line-height:1.5;color:#0000ff;">this</span>, [e]);
                });
            });
        };
             
    };
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If Mozilla is used</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#0000ff;">if</span> ( jQuery.browser.mozilla || jQuery.browser.opera ) {
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Use the handy event callback</span><span style="line-height:1.5;color:#008000;"> </span>        document.addEventListener( "DOMContentLoaded", jQuery.ready, <span style="line-height:1.5;color:#0000ff;">false</span> );
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If IE is used, use the excellent hack by Matthias Miller</span><span style="line-height:1.5;color:#008000;"> </span>    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited</span><span style="line-height:1.5;color:#008000;"> </span>    } <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( jQuery.browser.msie ) {
     
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Only works if you document.write() it</span><span style="line-height:1.5;color:#008000;"> </span>        document.write("&lt;scr" + "ipt id=__ie_init defer=true " +
            "src=//:&gt;&lt;\/script&gt;");
     
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Use the defer script hack</span><span style="line-height:1.5;color:#008000;"> </span>        <span style="line-height:1.5;color:#0000ff;">var</span> script = document.getElementById("__ie_init");
        script.onreadystatechange = <span style="line-height:1.5;color:#0000ff;">function</span>() {
            <span style="line-height:1.5;color:#0000ff;">if</span> ( <span style="line-height:1.5;color:#0000ff;">this</span>.readyState == "complete" )
                jQuery.ready();
        };
     
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Clear from memory</span><span style="line-height:1.5;color:#008000;"> </span>        script = <span style="line-height:1.5;color:#0000ff;">null</span>;
     
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If Safari  is used</span><span style="line-height:1.5;color:#008000;"> </span>    } <span style="line-height:1.5;color:#0000ff;">else</span> <span style="line-height:1.5;color:#0000ff;">if</span> ( jQuery.browser.safari ) {
        <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Continually check to see if the document.readyState is valid</span><span style="line-height:1.5;color:#008000;"> </span>        jQuery.safariTimer = setInterval(<span style="line-height:1.5;color:#0000ff;">function</span>(){
            <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> loaded and complete are both valid states</span><span style="line-height:1.5;color:#008000;"> </span>            <span style="line-height:1.5;color:#0000ff;">if</span> ( document.readyState == "loaded" ||
                document.readyState == "complete" ) {
     
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If either one are found, remove the timer</span><span style="line-height:1.5;color:#008000;"> </span>                clearInterval( jQuery.safariTimer );
                jQuery.safariTimer = <span style="line-height:1.5;color:#0000ff;">null</span>;
     
                <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> and execute any waiting functions</span><span style="line-height:1.5;color:#008000;"> </span>                jQuery.ready();
            }
        }, 10);
    }
    <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> A fallback to window.onload, that will always work</span><span style="line-height:1.5;color:#008000;"> </span>    jQuery.event.add( window, "load", jQuery.ready );
     
};</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347961.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第二个属于很大的代码断了，前半部分大体明白是在jQuery对象上绑定常见事件，但是由于不了解bind、event的实现方式所以这部分代码具体实现逻辑不是很明白，所以能放下就暂且放一放。后半部分代码则是对jQuery.ready方法的处理，在何时页面文档加载完成可以执行jQuery.ready方法了。这一段比较容易看懂(注释很多)，主要是跨浏览器的兼容。</p>
<pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;color:#333333;font-size:14px;line-height:25px;text-align:left;background-color:#ffffff;">Mozilla 浏览器算是比较简单，safari虽然代码多一点，但是实现逻辑也容易懂-就是不停的检查document的readyState，如果是loaded或 complete 则可以触发jQuery.ready了。而ie则用到了一个excellent hack。最后的一段代码 jQuery.event.add(window, "load",jQuery.ready);是一般能工作的模式。所以这里猜测jQuery.ready 应该也是run once(运行一次)的吧。</pre><p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">&nbsp;</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第三个：主要是jQuery对象上的Ajax事件定义？</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Attach a bunch of functions for handling common AJAX events</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">new</span> <span style="line-height:1.5;color:#0000ff;">function</span>(){
    <span style="line-height:1.5;color:#0000ff;">var</span> e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess".split(',');
     
    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i = 0; i &lt; e.length; i++ ) <span style="line-height:1.5;color:#0000ff;">new</span> <span style="line-height:1.5;color:#0000ff;">function</span>(){
        <span style="line-height:1.5;color:#0000ff;">var</span> o = e[i];
        jQuery.fn[o] = <span style="line-height:1.5;color:#0000ff;">function</span>(f){
            <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">this</span>.bind(o, f);
        };
    };
};</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347961.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">这一个代码与第二个的前半部分代码相似。实现的功能不是很理解，等后面读一些代码后再进一步理解。但是jQuery.fn[o] = function(){}的用法应该能看懂。注意是字幕o而不是数字0。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">在这个代码之前还有一段IE兼容的代码，为的是在IE下创建XMLHttpRequest Ajax请求对象：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> If IE is used, create a wrapper for the XMLHttpRequest object</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">if</span> ( jQuery.browser.msie )
    XMLHttpRequest = <span style="line-height:1.5;color:#0000ff;">function</span>(){
        <span style="line-height:1.5;color:#0000ff;">return</span> <span style="line-height:1.5;color:#0000ff;">new</span> ActiveXObject(
            navigator.userAgent.indexOf("MSIE 5") &gt;= 0 ?
            "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
        );
    };</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347961.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">IE浏览器可是有自己的XMLHttpRequest 创建模式的，经常接触Ajax的应该对此不陌生的。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">总结：</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">到此为止，我们脑海里应该清晰了jQuery部分代码，并且对jQuery中用到的部分js特性有了了解。浏览器版本判断、CSS盒子模型的判断(这个在jQuery里起到的什么作用呢？)、页面加载完成的兼容代码、XMLHttpRequest兼容代码、extend的实现、jQuery.method = jQuery.fn.method = function(){ ... }的使用方式。接下来，需要更深层次的去理解和分析jQuery.extend( ... ) 代码段中所包含的代码。</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-two.html]]></link>
<title><![CDATA[（二）解决前6条]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 14:37:43 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">1.window.undefined = window.undefined</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">make the global window.undefined as real undefined value.</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">未定义，当属性没有定义过(不存在)时访问该属性则会返回值：未定义。虽然window.undefined一定会被浏览器定义成了"未定义"的值，但是谁会知道哪个别有用心的浏览器（火狐）是个例外呢。所以，这一段代码做了双重保证。定义了window.undefined属性为未定义的浏览器中执行这一句代码没有任何改变，在没有定义window.undefined属性的浏览器中执行后window.undefined 则会被定义并且赋值成未定义。网上有提到，对于没有定义window.undefined属性的浏览器判断一个是否是未定已要使用如下判断 if(window.p == 'undefined')。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">&nbsp;</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">2.对$元符号的处理。</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Map over the $ in case of overwrite</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">if</span> ( $ )
    jQuery._$ = $;<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Map the jQuery namespace to the '$' one</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">var</span> $ = jQuery;</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347951.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">如果已经定义过美元符号了，则将已经定义存在的美元符号赋值到jQuery._$上。然后重新定义美元符号$为jQuery。这里需要特别的注意在将jQuery赋值到$上时使用了var 重新定义$美元符号的语法。否则将会把jQuery._$一起覆盖：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Map over the $ in case of overwrite</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">if</span> ( $ )
    jQuery._$ = $;<span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> Map the jQuery namespace to the '$' one</span><span style="line-height:1.5;color:#008000;"> </span>$ = jQuery; <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;">这一句将会导致 jQuery._$ = $ = jQuery的效果，而不是 jQuery._$ = old $ , $ = jQuery 的效果。</span></pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347951.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">&nbsp;</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">3.jQuery(a,c) 与 jQuery.fn = jQuery.prototype =&nbsp;{ ... }</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">看着看着发现，jQuery(a,c)中的实现引用了后面定义的很多函数，而jQuery.fn = jQuery.prototype =&nbsp;{...}中代码量也很大，而且也引用到了后面定义的函数。相反，jQuery.extend 的定义则相对简单很多，于是选择阅读jQuery.extend中的代码。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">&nbsp;</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">4.jQuery.extend 方法</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">在分析jQuery.extend之前，我们需要理解jQuery.fn = jQuery.prototype ={ ... } 的写法</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">js中的prototype在面向对象方面有很大作用。你可以在网上查阅一下这方面的相关资料。经由jQuery.fn = jQuery.prototype =&nbsp;{ ... } 我们在之后对jQuery拓展方法时就可以使用 jQuery.method = jQuery.fn.method = function(){ ... }的形式了。jQuery.method 仅会让方法成为jQuery本身的方法，而jQuery.fn.method则仅会让方法成为jQuery对象的方法。两者同时使用才会达到jQuery自身和jQuery对象同时拥有该方法的效果。我们通过如下代码来演示这种效果：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">function</span> jQuery(){} <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 一个空的叫做jQuery的类型</span><span style="line-height:1.5;color:#008000;"> </span>  <span style="line-height:1.5;color:#0000ff;">var</span> omygod = <span style="line-height:1.5;color:#0000ff;">function</span>(){alert('God bless you.');} <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;">定一个函数</span><span style="line-height:1.5;color:#008000;"> </span>  
jQuery.me = omygod;
  
alert(jQuery.me); <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 弹窗显示 function(){alert('God bless you.');}</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">var</span> jq = <span style="line-height:1.5;color:#0000ff;">new</span> jQuery();
alert(jq.me);  <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 弹窗显示 undefined</span><span style="line-height:1.5;color:#008000;"> </span>  
jQuery.fn = jQuery.prototype;
jQuery.fn.me = omygod;
alert(jq.me);  <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 弹窗显示 function(){alert('God bless you.');}</span><span style="line-height:1.5;color:#008000;"> </span> 
jQuery.fn.you = 'hi'
alert(jq.you)  <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 弹窗显示 hi</span><span style="line-height:1.5;color:#008000;"> </span>alert(jQuery.you) <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;">弹窗显示 undefined</span></pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347951.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">如果你有更多的疑问点，请提出你的问题或者自行编写代码进行验证。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">接下来我们看一下jQuery中伟大的extend方法，这个方法功能强大实现怎么可能是这样的？</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';">jQuery.extend = jQuery.fn.extend = <span style="line-height:1.5;color:#0000ff;">function</span>(obj,prop) {
    <span style="line-height:1.5;color:#0000ff;">if</span> ( !prop ) { prop = obj; obj = <span style="line-height:1.5;color:#0000ff;">this</span>; }
    <span style="line-height:1.5;color:#0000ff;">for</span> ( <span style="line-height:1.5;color:#0000ff;">var</span> i <span style="line-height:1.5;color:#0000ff;">in</span> prop ) obj[i] = prop[i];
    <span style="line-height:1.5;color:#0000ff;">return</span> obj;
};</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347951.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;"><span style="line-height:1.8;color:#ccffcc;font-size:12px;font-family:楷体;">真是不知者有罪啊，如果不知道怎么实现的将会造成多大的罪孽。——by kwjlk</span></p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">jQuery.extend实现虽然简单，但是其强大的功能正是印证了js的强大。首先这里面有三个重要点：</p>
<ol style="margin-left:2em;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">js中的真假世界观，即if(!prop)的判断何时是if(true)何时是if(false)。</li>
<li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">js中可以以数组形式访问和赋值对象的属性。</li>
<li style="list-style-type:decimal;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">js 中 for..in 数组遍历(基于第二点，则也可以遍历对象的属性)</li>
</ol>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">&nbsp;</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">对于第一点，在js中 null , undefined , false , 字符串空,数字0 均为false，非false则为true。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">接下来研究如上代码的实现逻辑。首先第一个判断if(!prop){prop = obj,obj = this} 这是的当!prop计算为真(一般为没有传入prop参数)时则将obj对象的属性拓展到extend的调用者身上。接下来我们通过代码分析extend的可能调用场景：</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">先看如下两段代码：</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第一段代码：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">var</span> obj = {}
obj['name'] = 'object';
alert(obj.name); <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 弹窗显示 object</span><span style="line-height:1.5;color:#008000;"> </span><span style="line-height:1.5;color:#0000ff;">var</span> nonObj = "12";
nonObj['name'] = 'none object';
alert(nonObj.name);  <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;">弹窗显示 none object</span></pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347951.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">以上代码说明，对于非对象性的变量，使用var['key'] = 'value' 的形式并不报错，但是调用var.key时key是undefined。这就是，在extend真正对变量进行属性拓展时，如果目的变量是非对象变量则等同于没有做任何操作。于是在讨论中可以把extend最终作用对象分为对象和非对象，而对非对象的讨论则没有意义。至于那些是非对象呢？我们通过如下代码展示：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#008000;">//此段代码有待商榷，希望有志认识能够提供指正</span><span style="line-height:1.5;color:#0000ff;">var</span> nonObj;
nonObj = <span style="line-height:1.5;color:#0000ff;">null</span>;
nonObj = undefined;
nonObj = 'this is a string';
nonObj = 100;
nonObj = 100.9;
nonObj = ture;
nonObj = <span style="line-height:1.5;color:#0000ff;">false</span>;</pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347951.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">&nbsp;</p>
<div class="code panel" style="color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;">&nbsp;</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">第二段代码：</p>
<div class="cnblogs_code" style="font-family:'Courier New';border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#cccccc;border-right-color:#cccccc;border-bottom-color:#cccccc;border-left-color:#cccccc;border-image:initial;padding-top:5px;padding-right:5px;padding-bottom:5px;padding-left:5px;word-break:break-all;overflow-x:auto;overflow-y:auto;margin-top:5px;margin-right:0px;margin-bottom:5px;margin-left:0px;color:#333333;text-align:left;"><pre style="margin-top:0px;margin-bottom:0px;white-space:pre-wrap;word-wrap:break-word;max-width:600px;font-family:'Courier New';"><span style="line-height:1.5;color:#0000ff;">var</span> nonObj;
nonObj = <span style="line-height:1.5;color:#0000ff;">null</span>;
nonObj = undefined;
nonObj = 100;
nonObj = 100.9;
nonObj = ture;
nonObj = <span style="line-height:1.5;color:#0000ff;">false</span>;<span style="line-height:1.5;color:#0000ff;">for</span>(<span style="line-height:1.5;color:#0000ff;">var</span> i <span style="line-height:1.5;color:#0000ff;">in</span> nonObj){
    alert('not here'); <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;">这段代码不会被执行</span><span style="line-height:1.5;color:#008000;"> </span>}
nonObj = 'st';<span style="line-height:1.5;color:#0000ff;">for</span>(<span style="line-height:1.5;color:#0000ff;">var</span> i <span style="line-height:1.5;color:#0000ff;">in</span> str){
    alert(i+'-'+str[i]); <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;"> 弹窗显示 0-s 1-t</span><span style="line-height:1.5;color:#008000;"> </span>}
nonObj[1] = 'b';
alert(nonObj); <span style="line-height:1.5;color:#008000;">//</span><span style="line-height:1.5;color:#008000;">弹窗显示 st</span></pre><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a href="http://www.cnblogs.com/klvk/archive/2012/02/12/2347951.html" style="outline-style:none;outline-width:initial;outline-color:initial;text-decoration:none;color:#3d81ee;">复制代码</a></span></div>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">如上代码说明，对于非字符串的非对象变量，for ... in 也不会起到任何效果，而字符串就是一个字符数组。并且，字符串变量不能通过str[index] = char 的形式改变其值。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">总结extend的可能调用场景为</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">&nbsp;</p>
<div class="table-wrap" style="color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;line-height:25px;text-align:left;"><table class="confluenceTable" style="margin-top:0px;margin-right:auto;margin-bottom:0px;margin-left:auto;border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;"><tbody><tr><th class="confluenceTh" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">obj</th>
<th class="confluenceTh" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">prop</th>
<th class="confluenceTh" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">结果</th>
</tr>
<tr><td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">对象</td>
<td class="confluenceTd" align="left" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">对象</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">将prop的属性拓展到对象obj身上</td>
</tr>
<tr><td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">对象</td>
<td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">非对象真(即if(!prop)不成立)，且不为字符串</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">无操作</td>
</tr>
<tr><td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">对象</td>
<td class="confluenceTd" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">字符串</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">对象obj以obj[index] = char 的形式存储字符串，但对象obj不会变成字符串</td>
</tr>
<tr><td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">字符串</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">任意值</td>
<td class="confluenceTd" colspan="1" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;border-top-color:#c0c0c0;border-right-color:#c0c0c0;border-bottom-color:#c0c0c0;border-left-color:#c0c0c0;border-image:initial;border-collapse:collapse;padding-top:3px;padding-right:3px;padding-bottom:3px;padding-left:3px;">obj仍旧为字符串，值无变化</td>
</tr>
</tbody>
</table>
</div>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">当prop为非对象假时(即if(!prop)成立)，obj指向当前extend的调用者，prop指向原obj对象。再次套用上表。</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">&nbsp;</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">8. new function(){} 的神奇用法</p>
<p style="line-height:25px;margin-top:12px;margin-right:auto;margin-bottom:12px;margin-left:auto;color:#333333;font-family:Georgia, 'Times New Roman', Times, san-serif;font-size:14px;text-align:left;">当对象对象对象，脑子里盘旋的全是对象的时候。在Chrome控制台敲入了一段测试代码new function(){} ，Chrome控制台给出的返回结果为Object。顿时，我就明白了new function(){} 与 (function(){})()实现了一样的效果(这里涉及到了js中的匿名函数，还可以发散一下js中的闭包)，即定义函数后立即执行。但两者之间孰优属劣呢？</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/jquery-1pointzero-code-structure.html]]></link>
<title><![CDATA[（一）划分JQuery的代码结构]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 14:24:23 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p><img src="/Upload/EditorImage/image/klvoek/201202/20120212142333_1402.gif" title="jQuery1.0代码结构划分" alt="jQuery1.0代码结构划分" border="0" /></p>
<p><p style="font-size:13px;line-height:17px;color:#333333;background-image:none;background-attachment:initial;background-origin:initial;background-clip:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;margin-bottom:10px;font-family:Arial, Helvetica, FreeSans, sans-serif;text-align:left;">将JQuery1.0的全部代码，以第一层为准收起。代码总行数减少，可以对代码大体结构进行梳理和了解。</p>
<p style="font-size:13px;line-height:17px;color:#333333;background-image:none;background-attachment:initial;background-origin:initial;background-clip:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;margin-bottom:10px;font-family:Arial, Helvetica, FreeSans, sans-serif;text-align:left;">此时代码行数为19行，浏览第一遍后可以看到几个令人好奇的地方和一些简单的外层代码。列举如下：</p>
<p style="font-size:13px;line-height:17px;color:#333333;background-image:none;background-attachment:initial;background-origin:initial;background-clip:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;margin-bottom:10px;font-family:Arial, Helvetica, FreeSans, sans-serif;text-align:left;">令人好奇的代码：</p>
<ul style="margin-top:1em;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-left:3em;font-size:13px;line-height:17px;padding-top:0px;color:#333333;font-family:Arial, Helvetica, FreeSans, sans-serif;text-align:left;"><li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">window.undefined = window.undefined 为什么要这么写？</li>
<li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">new function(){} 这种不是自执行函数( (即function(args){...code ... })(arg) 的形式)，那这样写有什么效果啊？</li>
</ul>
<p style="font-size:13px;line-height:17px;color:#333333;background-image:none;background-attachment:initial;background-origin:initial;background-clip:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;margin-bottom:10px;font-family:Arial, Helvetica, FreeSans, sans-serif;text-align:left;">简单的外层代码：</p>
<ul style="margin-top:1em;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-left:3em;font-size:13px;line-height:17px;padding-top:0px;color:#333333;font-family:Arial, Helvetica, FreeSans, sans-serif;text-align:left;"><li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">对美元符号$的处理。</li>
<li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">Ajax插件(代码量不少，但是从模块的角度看，可以显然的知道后面的代码是属于Ajax插件的，可以在看代码的时候排后看。)</li>
</ul>
<p style="font-size:13px;line-height:17px;color:#333333;background-image:none;background-attachment:initial;background-origin:initial;background-clip:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;margin-bottom:10px;font-family:Arial, Helvetica, FreeSans, sans-serif;text-align:left;">最后对这个代码整体结构进行梳理，理出看代码时的先后顺序。列举如下：</p>
<ol style="margin-top:1em;margin-right:0px;margin-bottom:1em;margin-left:0px;padding-left:3em;font-size:13px;line-height:17px;padding-top:0px;color:#333333;font-family:Arial, Helvetica, FreeSans, sans-serif;text-align:left;"><li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">window.undefined = window.undefined - why?</li>
<li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">JQuery(a,c) 的定义</li>
<li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">$ 美元符号的处理</li>
<li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">JQuery.fn 的定义</li>
<li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">JQuery.extend 的定义</li>
<li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">使用JQuery.extend 扩展JQuery功能</li>
<li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">Ajax插件的定义</li>
<li style="font-size:10pt;line-height:13pt;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;">非常神奇的new function的使用</li>
</ol>
<p style="font-size:13px;line-height:17px;color:#333333;background-image:none;background-attachment:initial;background-origin:initial;background-clip:initial;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;margin-bottom:0px;font-family:Arial, Helvetica, FreeSans, sans-serif;text-align:left;">对于如上8部分的梳理，前5条和第8条需要逐次解决。6和7属于真正的代码大头。</p>
</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/12/maccms-short-open-tag.html]]></link>
<title><![CDATA[maccms 安装时提示call to undefined function stepB]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 12 Feb 2012 03:38:17 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p>maccms 苹果CMS，是一个在线影视的PHP程序。默认皮肤挺好看的。但是在JB安装过程中竟然发生了 call to undefined function stepB。话又说回来，这是为什么呢？好在php+apache的提示信息给出的够详细。于是打开install/index.php 查看代码。慨叹程序员真是有责任心啊，所有代码都在方便查看。于是找到了stepB 函数 以及 switch跳转语句。结果发现stepB这不是好好的么？没办法尝试一下更改stepB为 myStep。额，掩耳盗铃了。没办法，看了一下总共四个步骤对应的函数代码，发现stepB十分强大，设置配置文件生成数据库都在里面。等于其他几步可有可无。于是删除stepA 我直接让你stepB好了吧？</p>
<p style="font-size:64px;">成功安装。</p>
<p>接下来去后台管理吧。后台打开，点管理视频。竟然又来错误：<span class="Apple-style-span" style="font-family:Simsun, tahoma, verdana, helvetica;font-size:medium;line-height:normal;">syntax error, unexpected T_ELSE in&nbsp;<b>D:\xampp\htdocs\maccms7\admin\admin_sql.php</b>&nbsp;on line&nbsp;<b>48。</b><span class="Apple-style-span" style="font-family:''sans serif', tahoma, verdana, helvetica';font-size:small;"><span class="Apple-style-span" style="font-size:12px;line-height:18px;">这是怎么回事呢？既然提示错误信息很详细，指明了在哪个行上。于是打开这个文件查看48行出的else。没有问题啊。于是前前后后的那么一看~ &nbsp;比我注意到有地方试用了&lt;? ?&gt; short_open_tag 的标记。这是心里有谱了，是不是php没有把short_open_tag设置程true啊？查看php.ini果真如此。本想改php配置文件，一想还要重启apahce。就改代码文件好了。好的吗，改为&lt;?php &nbsp;?&gt;来,测试-ok了。</span></span></span></p>
<p>那么，之前找不到stepB的问题应该也是因为这个问题吧~</p>
<p>总的来说，maccms有四个文件里存在这个问题。而且每个文件就一个~ 源文件改过来就好了。</p>
<p>开什么玩笑，有很多个的其实，看样子还是老老实实该php.ini了。启用short_open_tag = On</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/5/dom-event-constructor-opera-translatation.html]]></link>
<title><![CDATA[opera 11.60 中的DOM 事件构造函数]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sun, 05 Feb 2012 11:32:56 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p>中午12点了，该吃饭了。发完这篇译文就去买菜。哈哈，</p>
<p>话说从 司徒正美 (新浪微博)的微博里看到了这篇文章<a href="http://my.opera.com/ODIN/blog/2011/11/08/dom-event-constructors-in-opera-11-60">http://my.opera.com/ODIN/blog/2011/11/08/dom-event-constructors-in-opera-11-60</a>&nbsp;于是今天又有了着落。没啥，开始翻译吧：</p>
<p><hr />
</p>
<p>在Divya的博客里提到 Opera11.60里有什么新功能，Opera现在支持DOM事件构造函数了。这提供了一个简单的方式去创建合模拟的和自定义的DOM事件。</p>
<p>为什么需要模拟的或者自定义的时间呢？模拟的事件在自动化测试中非常有用，或者开发随着用户输入而改变的用户界面。自定义事件在游戏开发中尤为重要，比如你可能需要在用户按下空格键的时候触发一个“射击”事件。</p>
<h3>创建模拟事件</h3>
<p>先让我们看一下过去我们是如何创建一个input元素的focus事件的。</p>
<p><pre class="brush:js;">var event = document.createEvent('Event');
event.initEvent('focus', false, true);
document.querySelector('input').dispatchEvent(event);</pre>首先我们通过createEvent()方法创建一个事件对象，接着我们使用InitEvent对其进行初始化，然后通过dispatchEvent()把在input元素上进行触发。接下来让我们用Event()构造函数实现同样的功能并进行比较。<pre class="brush:js;">var event = new Event('focus', {bubbles:false,cancelable:true});
document.querySelector('input').dispatchEvent(event);</pre></p>
<p>我们现在只需两行代码：一行来创建这个事件，一行触发它。我们的第一个参数是我们需要触发的事件的名称。第二参数是可选参数。这个参数是一个字典，一个定义事件属性的key-value集合。这些属性由于你创建的事件类型的不同而有所区别。例如一个鼠标事件MouseEvent，具有screenX和screenY属性。在这个例子里，我们仅设置bubbles和cancelable属性。</p>
<h3>自定义事件</h3>
<p>自定义事件与模拟事件基本相同，但是要使用CustomEvent()构造函数。在如下的例子里，我们创建并触发了当前文档上的一个ripple事件。</p>
<p><pre class="brush:js;">document.dispatchEvent( new CustomEvent('ripple', {x:40, y:20}) );</pre>上面的例子展示了使用事件构造函数的一个好处：为dispatchEvent()方法创建自定义事件的功能。</p>
<p>通过切换到最新的事件触发的方式，DOM事件构造函数为我们提供了极大的灵活性和代码整洁性。</p>
<h3>浏览器支持</h3>
<p>Opera不是支持DOM事件构造函数的唯一一个浏览器。在最新的WebKit浏览器，包含最新版本的Chrome都添加了DOM事件构造函数的支持。</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/4/asp-on-error-resume-next.html]]></link>
<title><![CDATA[ASP如何使用On Error Resume Next]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Sat, 04 Feb 2012 22:32:58 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p>偶然间被问道ASP的基础知识，过去很久了，一点点都记不得了。回忆曾经写一点点ASP脚本的日子，调试使用的方法是：查看页面输出的错误信息，这个错误信息一般会给出出错的代码的位置和代码信息。如果没有以上信息的话，只好自己猜测是什么情况导致了错误（两种情况：新添加或修改了代码导致，没有任何修改。第一种是最多的情况，很好分析。第二种则麻烦一点了），在大概怀疑的位置增加判定代码。确定了出错的代码就好进行修改了。现在用习惯了vs的运行时调试功能，很想知道对于ASP有没有类似办法呢？好了，接下来翻译的正文：</p>
<p>原文链接：<a href="http://powerasp.net/content/new/on-error-resume-next.asp">http://powerasp.net/content/new/on-error-resume-next.asp</a></p>
<p>如果经常使用ASP或者动态服务器页面中使用VB脚本时，你会发现当你要实现有可能会抛出异常的代码并且你想在异常抛出时对其进行处理时进行错误检查是非常有必要的。比如要打开一个数据库连接或者向文件中写入文本。</p>
<p>通常在你的.asp脚本文件中产生了异常时，你的脚本处理过程会立即停止并向浏览器发送错误信息。如果你想在错误发生时继续执行你的asp页面，在你的.asp页面中加入如下代码</p>
<p>&lt;% On Error Resume Next %&gt;</p>
<p>在代码中设置忽略代码中所有的异常并继续向下执行并不是一个好想法。你也许应该这样处理代码中的异常：</p>
<p><pre class="brush:vb;">&lt;%
ConnectionString = "DBQ=c:\inetpub\wwwroot\mysite\data\mydatabase.mdb;Driver={Microsoft Access Driver (*.mdb)};"
  
'*** 下面的代码会检查你设置的ConnectionString，当连接字符串有问题时返回错误信息
Err.Clear
On Error Resume Next
Set ConnPasswords = Server.CreateObject("ADODB.Connection")
ConnPasswords.Open ConnectionString
  
If Err.Number &lt;&gt; 0 Then
  
  Response.Write (Err.Description&amp; "&lt;br&gt;&lt;br&gt;")
   
  Response.Write("这表示你设置的连接字符串：" &amp; vbCrLf)
  Response.Write("""ConnectionString"" 有点问题。&lt;br&gt;" &amp; vbCrLf)
  Response.End
  
End If
On Error GoTo 0
%&gt;</pre>我们把代码“On Error GoTo 0” 放在最后因为这会最终结束" On Error Resume Next"作用范围。这就是你想做的事情吧？这样你的应用中的任何错误都不会逃过你的眼睛。</p>
<p>下面是另外一个例子。在这个例子里我们的应用程序在用户登录网站时把用户信息记录到一个文本文件里。我们在这儿加上了“On Error Resume Next”，这样当我们没有写入文件的权限时不会看到任何错误。</p>
<p><pre class="brush:vb;">&lt;%
Set ObjMyFile = CreateObject("Scripting.FileSystemObject")
Err.Clear
On Error Resume Next
LogFileName = "aspprotect.log"
LogFileDirectory = "c:\somedirectory"
  
'打开文本文件，如果文本文件不存在，创建并将文本写道末尾。如果存在，直接写道末尾
Set WriteMyData = ObjMyFile.OpenTextFile(LogFileDirectory &amp; "\" &amp; LogFileName,8,True)
RowHeaderString = Session("User_ID") &amp; vbTab
RowHeaderString = RowHeaderString &amp; Session("Username") &amp; vbTab
RowHeaderString = RowHeaderString &amp; NOW &amp; vbTab
RowHeaderString = RowHeaderString &amp; Request.ServerVariables("REMOTE_ADDR")
WriteMyData.WriteLine(RowHeaderString)
WriteMyData.Close
On Error GoTo 0
%&gt;</pre>使用“On Error Resume Next”是要非常谨慎。增加“On Error GoTo 0” 的好处非常大，至少可以让你后面代码产生的错误不被忽略。</p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/2/a-link-with-trailing-slash.html]]></link>
<title><![CDATA[为什么链接之后多了一个反斜杠]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Thu, 02 Feb 2012 15:56:09 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p>a标签href中的连接如果是到子目录的链接，最好在其后加上一个反斜杠。</p>
<p>偶然在群里有人问，为何编辑器在插入的链接中在http://www.tageta.com/me 之后增加了一个反斜杠'/'，变成了 http://www.tageta.com/me/ ，他想把反斜杠去掉，这样看起来就美观了。当时也是颇感神奇，并没有想到这里也小有文章。在今天查看w3school中对于html的文档时，发现在对HTML links的说明下有一个非常有意思的Note：（原文链接:<a href="http://www.w3schools.com/html/html_links.asp">http://www.w3schools.com/html/html_links.asp</a>）</p>
<p></p>
<pre class="brush:plain;">Note: Always add a trailing slash to subfolder references.&nbsp;</pre><pre class="brush:plain;">If you link like this: href="http://www.w3schools.com/html",</pre><pre class="brush:plain;"> you will generate two requests to the server, the server&nbsp;</pre><pre class="brush:plain;">will first add a slash to the address, and then create a new</pre><pre class="brush:plain;"> request like this: href="http://www.w3schools.com/html/".</pre><p>大意是说：<span style="color:#333333;font-family:Arial, Helvetica, FreeSans, sans-serif;font-size:13px;line-height:17px;text-align:left;">&lt;a&gt;标签的href属性，最好在对子目录(即形如:http://www.tageta.com/me http://www.tageta.com/you/rock 的请求)的连接之后加上一个反斜杠。如果你使用href="http://www.tageta.com/youha"的请求，你将会产生两个请求到服务器，服务器首先在地址后则增加一个反斜杠，然后产生对href="http://www.tageta.com/youha/" 的请求。</span></p>
<p style="text-align:left;"><span style="color:#333333;font-family:'Arial, Helvetica, FreeSans, sans-serif';font-size:x-small;"><span style="line-height:17px;"><br />
</span></span></p>
<p style="text-align:left;"><span style="color:#333333;font-family:'Arial, Helvetica, FreeSans, sans-serif';font-size:x-small;"><span style="line-height:17px;">定位问题：</span></span></p>
<p style="text-align:left;"><span style="color:#333333;font-family:'Arial, Helvetica, FreeSans, sans-serif';font-size:x-small;"><span style="line-height:17px;">为什么链接之后多了一个反斜杠</span></span></p>
<p></p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/2/html-annotate-js-mixed.html]]></link>
<title><![CDATA[通过HTML注释标签，屏蔽不支持js脚本的代码原理是这样的啊]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Thu, 02 Feb 2012 00:34:00 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p>有以下html代码</p>
<p></p>
<pre class="brush:xhtml;">&lt;html&gt;
&lt;head&gt;&lt;/head&gt;
&lt;body&gt;
	&lt;script type="text/javascript"&gt;
	&lt;!--
	function displayMsg()
	{
	alert("Hello World!")
	}
	//--&gt;
	&lt;/script&gt; 
&lt;/body&gt;
&lt;/html&gt;</pre><p>script中的脚本内容可以让支持js脚本的浏览器正确解析其中的脚本内容，让不支持js脚本的浏览器把该内容当作普通文本不作处理。刚刚开始的时候看到这个写法，心理面也就哦是这样啊的感觉。今天遇到Google问题&lt;你真的了解HTML吗？ 雅虎面试题，参考连接：<a href="http://www.imf7.com/archives/107">http://www.imf7.com/archives/107 </a>&gt; 才开始抓耳挠腮的在W3CSchool看基本HTML标签定义。头一个就是&lt;!--&gt;标签。一甩头恍然大悟，哦，原来是这么回事啊：</p>
<p><ul><li>&nbsp; &nbsp; 对于支持js脚本的浏览器，//--&gt; 由于双反斜杠"//"在js中代表单行注释，于是&lt;!--的闭合标签--&gt;被优先解析成了js的注释文本。于是&lt;!--没有闭合标签了，所以该端js能够被支持js的浏览器正确读取运行。</li>
<li>&nbsp; &nbsp; 对于不支持js脚本的浏览器，//--&gt; 中的双反斜杠没有特殊意义，仅仅是普通文本耳。于是&lt;!-- --&gt; 之间的js内容最终被不支持js脚本的浏览器和谐为HTML 注释标签&lt;!-- --&gt; 之间的注释文本了。</li>
</ul>
</p>
<p></p>]]></description>
</item>

<item>
<link><![CDATA[http://www.itivy.com/klvoek/archive/2012/2/1/get-current-category-id.html]]></link>
<title><![CDATA[wordpress 获取当前分类的ID]]></title>
<author><![CDATA[klvoek]]></author>
<category><![CDATA[]]></category>
<pubDate>Wed, 01 Feb 2012 20:32:51 GMT</pubDate>
<guid><![CDATA[]]></guid>
<description><![CDATA[<p>在编写WordPress中使用的页面代码时，总会想在当前页取一些值。比如：博客标题，博客作者，当前页面时选中的分类。在网上看到了一段代码，自己试了试还是能够达到目的(取浏览当前页时选中的分类)：</p>
<p></p>
<pre class="brush:php;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$current_cat_id = 0;
			if (is_category()) {
				global $wp_query;
				$current_cat_id = $wp_query-&gt;get_queried_object_id();
			}</pre><p>参考连接：<a href="http://murray.cn/index.php/2011/04/wordpress-cat-id-is_category-get_queried_object_id/" target="_blank">http://murray.cn/index.php/2011/04/wordpress-cat-id-is_category-get_queried_object_id/</a></p>]]></description>
</item>


</channel>
</rss>

