一直不知道load还有这个用法,看源码才发现,汗
今天看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, );}});return this;}
页:
[1]