六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 51|回复: 0

一直不知道load还有这个用法,看源码才发现,汗

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-1-29 11:25:06 | 显示全部楼层 |阅读模式
今天看JQuery源码中,无意中发现load源码中有如下代码:
 

var off = url.indexOf(" ");if ( off >= 0 ) {var selector = url.slice(off, url.length);url = url.slice(0, off);} 
感觉很是奇怪,以空格分割URL干嘛呢?
 
查看源码,发现好像空格后可以跟选择符,然后就查了下API,果真...
 

jQuery 1.2 中,可以指定选择符,来筛选载入的 HTML 文档,DOM 中将仅插入筛选出的 HTML 代码。语法形如 "url #some > selector" 用法如:

$("#links").load("/Main_Page #p-Getting-Started li"); 附上源码:
 

load: function( url, params, callback ) {if ( typeof url !== "string" )return this._load( url );var off = url.indexOf(" ");if ( off >= 0 ) {var selector = url.slice(off, url.length);url = url.slice(0, off);}// Default to a GET requestvar type = "GET";// If the second parameter was providedif ( params )// If it's a functionif ( jQuery.isFunction( params ) ) {// We assume that it's the callbackcallback = params;params = null;// Otherwise, build a param string} else if( typeof params === "object" ) {params = jQuery.param( params );type = "POST";}var self = this;// Request the remote documentjQuery.ajax({url: url,type: type,dataType: "html",data: params,complete: function(res, status){// If successful, inject the HTML into all the matched elementsif ( status == "success" || status == "notmodified" )// See if a selector was specifiedself.html( selector ?// Create a dummy div to hold the resultsjQuery("<div/>")// inject the contents of the document in, removing the scripts// to avoid any 'Permission Denied' errors in IE.append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""))// Locate the specified elements.find(selector) :// If not, just inject the full resultres.responseText );if( callback )self.each( callback, [res.responseText, status, res] );}});return this;}  
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表