开发学院网页设计JavaScript jQuery.API源码深入剖析以及应用实现 - 核心函数... 阅读

jQuery.API源码深入剖析以及应用实现 - 核心函数篇

 2010-09-14 13:38:00 来源:WEB开发网   
核心提示: 关键看到这样的一句代码,selector = jQuery.clean( [ match[1] ], context ); 继续查看clean都做了些什么:clean:function(elems,context,fragment){context=context||document;//

关键看到这样的一句代码,selector = jQuery.clean( [ match[1] ], context ); 继续查看clean都做了些什么:

  clean:function(elems,context,fragment){
    context=context||document;
    //!context.createElementfailsinIEwithanerrorbutreturnstypeof'object'
    if(typeofcontext.createElement==="undefined")
      context=context.ownerDocument||context[0]&&context[0].ownerDocument||document;
    //Ifasinglestringispassedinandit'sasingletag
    //justdoacreateElementandskiptherest
    if(!fragment&&elems.length===1&&typeofelems[0]==="string"){
      varmatch=/^<(w+)s*/?>$/.exec(elems[0]);
      if(match)
        return[context.createElement(match[1])];
    }
    varret=[],scripts=[],div=context.createElement("div");
    jQuery.each(elems,function(i,elem){
      if(typeofelem==="number")
        elem+='';
      if(!elem)
        return;
      //ConverthtmlstringintoDOMnodes
      if(typeofelem==="string"){
        //Fix"XHTML"-styletagsinallbrowsers
        elem=elem.replace(/(<(w+)[^>]*?)/>/g,function(all,front,tag){
          returntag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i)?
            all:
            front+"></"+tag+">";
        });
        //Trimwhitespace,otherwiseindexOfwon'tworkasexpected
        vartags=jQuery.trim(elem).toLowerCase();
        varwrap=
          //optionoroptgroup
          !tags.indexOf("<opt")&&
          [1,"<selectmultiple='multiple'>","</select>"]||
          !tags.indexOf("<leg")&&
          [1,"<fieldset>","</fieldset>"]||
          tags.match(/^<(thead|tbody|tfoot|colg|cap)/)&&
          [1,"<table>","</table>"]||
          !tags.indexOf("<tr")&&
          [2,"<table><tbody>","</tbody></table>"]||
          //<thead>matchedabove
          (!tags.indexOf("<td")||!tags.indexOf("<th"))&&
          [3,"<table><tbody><tr>","</tr></tbody></table>"]||
          !tags.indexOf("<col")&&
          [2,"<table><tbody></tbody><colgroup>","</colgroup></table>"]||
          //IEcan'tserialize<link>and<script>tagsnormally
          !jQuery.support.htmlSerialize&&
          [1,"div<div>","</div>"]||
          [0,"",""];
        //Gotohtmlandback,thenpeeloffextrawrappers
        div.innerHTML=wrap[1]+elem+wrap[2];
        //Movetotherightdepth
        while(wrap[0]--)
          div=div.lastChild;
        //RemoveIE'sautoinserted<tbody>fromtablefragments
        if(!jQuery.support.tbody){
          //Stringwasa<table>,*may*havespurious<tbody>
          vartbody=!tags.indexOf("<table")&&tags.indexOf("<tbody")<0?
            div.firstChild&&div.firstChild.childNodes:
            //Stringwasabare<thead>or<tfoot>
            wrap[1]=="<table>"&&tags.indexOf("<tbody")<0?
              div.childNodes:
              [];
          for(varj=tbody.length-1;j>=0;--j)
            if(jQuery.nodeName(tbody[j],"tbody")&&!tbody[j].childNodes.length)
              tbody[j].parentNode.removeChild(tbody[j]);
          }
        //IEcompletelykillsleadingwhitespacewheninnerHTMLisused
        if(!jQuery.support.leadingWhitespace&&/^s/.test(elem))
          div.insertBefore(context.createTextNode(elem.match(/^s*/)[0]),div.firstChild);
        
        elem=jQuery.makeArray(div.childNodes);
      }
      if(elem.nodeType)
        ret.push(elem);
      else
        ret=jQuery.merge(ret,elem);
    });
    if(fragment){
      for(vari=0;ret[i];i++){
        if(jQuery.nodeName(ret[i],"script")&&(!ret[i].type||ret[i].type.toLowerCase()==="text/javascript")){
          scripts.push(ret[i].parentNode?ret[i].parentNode.removeChild(ret[i]):ret[i]);
        }else{
          if(ret[i].nodeType===1)
            ret.splice.apply(ret,[i+1,0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))));
          fragment.appendChild(ret[i]);
        }
      }
      
      returnscripts;
    }
    returnret;
  }

上一页  1 2 3 4 5 6 7 8  下一页

Tags:jQuery API 源码

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接