jQuery技术

jQuery之工具函数(2)

字号+ 作者:H5之家 来源:H5之家 2017-07-15 09:02 我要评论( )

jQuery.ajaxPrefilter()函数还可以只预处理指定数据类型(dataType)的AJAX请求的参数选项。jQuery代码如下所示: //设置AJAX的全局默认选项$.ajaxSetup( {url: /index.html , // 默认URLaysnc: false , // 默认同步

jQuery.ajaxPrefilter()函数还可以只预处理指定数据类型(dataType)的AJAX请求的参数选项。jQuery代码如下所示:

//设置AJAX的全局默认选项 $.ajaxSetup( { url: "/index.html" , // 默认URL aysnc: false , // 默认同步加载 type: "POST" , // 默认使用POST方式 headers: { // 默认添加请求头 "Author": "CodePlayer" , "Powered-By": "CodePlayer" } , error: function(jqXHR, textStatus, errorMsg){ // 出错时默认的处理函数 // 提示形如:发送AJAX请求到"/index.html"时出错[404]:Not Found alert( '发送AJAX请求到"' + this.url + '"时出错[' + jqXHR.status + ']:' + errorMsg ); } } ); // 指定预处理参数选项的函数,只预处理dataType为json或html类型的AJAX请求 $.ajaxPrefilter( "json html", function(options, originalOptions, jqXHR){ // options对象 包括accepts、crossDomain、contentType、url、async、type、headers、error、dataType等许多参数选项 // originalOptions对象 就是你为$.ajax()方法传递的参数对象,也就是 { url: "/index.php" } // jqXHR对象 就是经过jQuery封装的XMLHttpRequest对象(保留了其本身的属性和方法) if( options.data == null && options.type == "POST"){ options.type = "GET"; // 将请求方式改为GET options.headers = { }; // 清空自定义的请求头 } }); // 执行AJAX请求 $.ajax( { url: "/index.php" } ); // 执行AJAX请求(该请求会被预处理) $.ajax( { url: "action.php?type=json", dataType: "json" } );

以上就是jQuery.ajaxPrefilter() 函数使用详解的详细内容,更多请关注php中文网其它相关文章!

jQuery.isPlainObject()函数用于判断指定参数是否是一个纯粹的对象。

所谓"纯粹的对象",就是该对象是通过"{}"或"new Object"创建的。

该函数属于全局jQuery对象。

语法

jQuery 1.4 新增该静态函数。

jQuery.isPlainObject( object )

参数

参数

描述

object 任意类型需要进行判断的任意值。

注意:宿主对象(或其它被浏览器宿主环境使用的对象,以完成ECMAScript的执行环境)难以进行跨平台的特性检测。因此,对于这些对象的实例,$.isPlainObject()在不同的浏览器上可能得出不同的结果。

返回值

jQuery.isPlainObject()函数的返回值为Boolean类型,如果指定的参数是纯粹的对象,则返回true,否则返回false。

示例&说明

jQuery.isPlainObject()函数的jQuery示例代码如下:

//在当前页面内追加换行标签和指定的HTML内容 function w( html ){ document.body.innerHTML += "<br/>" + html; } w( $.isPlainObject( { } ) ); // true w( $.isPlainObject( new Object() ) ); // true w( $.isPlainObject( { name: "CodePlayer"} ) ); // true w( $.isPlainObject( { sayHi: function(){} } ) ); // true w( $.isPlainObject( "CodePlayer" ) ); // false w( $.isPlainObject( true ) ); // false w( $.isPlainObject( 12 ) ); // false w( $.isPlainObject( [ ] ) ); // false w( $.isPlainObject( function(){ } ) ); // false w( $.isPlainObject( document.location ) ); // false(在IE中返回true) function Person(){ this.name = "张三"; } w( $.isPlainObject( new Person() ) ); // false

以上就是jQuery.isPlainObject() 函数使用详解的详细内容,更多请关注php中文网其它相关文章!

jQuery.now()函数用于返回当前时间距1970年1月1日午夜所经过的毫秒数。

该函数的作用类似于new Date().getTime();

该函数属于全局jQuery对象。

语法

jQuery 1.4.3 新增该静态函数。

jQuery.now( )

返回值

jQuery.now()函数的返回值为Number类型,返回当前时间距1970年1月1日午夜所经过的毫秒数。

示例&说明

jQuery.now()函数的源代码部分如下:

now: function() { return +( new Date() ); }

以下jQuery示例代码用于演示jQuery.now()函数的具体用法:

//在当前页面内追加换行标签和指定的HTML内容 function w( html ){ document.body.innerHTML += "<br/>" + html; } w( $.now() ); // 返回当前时间的Unix时间戳

以上就是jQuery.now() 函数使用详解的详细内容,更多请关注php中文网其它相关文章!

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • jQuery代码优化:事件委托篇

    jQuery代码优化:事件委托篇

    2017-07-15 18:01

  • jQuery中使用.data()方法避免内存泄漏

    jQuery中使用.data()方法避免内存泄漏

    2017-07-13 15:47

  • javascript函数库:jQuery基础教程

    javascript函数库:jQuery基础教程

    2017-07-13 14:36

  • jQuery Mobile 入门教程

    jQuery Mobile 入门教程

    2017-07-13 13:01

网友点评
>