jQuery常用的工具方法

$.type()

用于确定JavaScript 对象的类型[[Class]] 。
和原生的typeof类似,都是用于判断数据类型,但typeof方法只能判断基本类型,$.type()更加细分,可以精确判断各种类型。
例:

console.log(typeof (a));//undefined
console.log(typeof ([]));//object
console.log(typeof ({}));//object
console.log($.type(a))//ReferenceError: a is not defined
console.log($.type([]));// array
console.log($.type({}));//object
console.log($.type(new Date()));// date
console.log($.type(/no/g));// regexp
console.log($.type(function () { }));// function
console.log($.type(new Number(10)));// number

$.trim()

$.trim()方法用于去除空格,如果不使用该方法,会连空格都打出来。结果是(string)而不是( string )

	var a = '  string  '
    console.log('('+$.trim(a)+')')//strinng

3、$.inArray()
描述: 在数组中查找指定值并返回它的索引(如果没有找到,则返回-1)
类似于原生数组中的indexOf()方法

语法: jQuery.inArray( value, array [, fromIndex ] )
value: 要查找的值。
array: 一个数组,通过它来查找。
fromIndex: 数组索引值,表示从哪里在开始查找。默认值是0,这将查找整个数组。
例:

//原生js如下
var array = ['a','b','c']
    console.log( array.indexOf('b') )//输出 1
//Query的$.inArray()工具方法如下
var array = ['a','b','c']
    console.log( $.inArray('b',array) )
jQuery.proxy( function, context )

改变this指向的方法
例:

    function show(a,b){
      console.log(a,b)
      console.log(this)
    }
    $.proxy(show,document)(1,2)

. p r o x y ( ) 这 个 方 法 接 收 的 两 个 参 数 第 一 个 是 函 数 , 另 一 个 是 t h i s 的 指 向 。 这 段 代 码 里 .proxy()这个方法接收的两个参数第一个是函数,另一个是this的指向。这段代码里 .proxy()this.proxy(show,document)已经是让show里的this指向了document了,但是它没有执行,所以要写成$.proxy(show,document)()这种形式才能调用和修改this。

$.proxy(show,document,1,2)
$.proxy(show,document,1,2)()
$.proxy(show,document)(1,2)

第一种是只修改this未调用函数,而后两种写法是等价的,都调用了函数,传参的方式也都对。而且,在里面进行传参就可以作为事件委托的执行函数存在。
示例如下:

    function show(a,b){
      console.log(a,b)
      console.log(this)
    }
    $(button).click($.proxy(show,document,1,2))

打出来的结果是1,2,document

$.each()遍历循环数组

例:

 var arr = [1, 23, 65, 8,];
 $.each(arr, function (index, ele) {
// ele 遍历当前的元素
// index 索引
 console.log(ele);
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值