你应该使用的20个有用的jQuery方法

原文地址:20 Helpful jQuery Methods you Should be Using
原文作者:Andrew Burgess

这篇文章介绍了jQuery中有用的20个方法,1-20编号如下,本文没有对照原文逐句翻译,这个20个方法名称,我其实就是用下面第14个map()的方式取出来的,亮点如下,firebug真是个离不开的好工具。

说到工具,那就顺便再提一下一个简洁的jQuery代码调试工具:jQueryPad

支持代码高亮,不支持代码辅助,小问题是这个工具带的jQuery.js的版本旧了,需要自己更新一份到它的目录。

(当然,我还是最喜欢firebug,特别是网页复杂的时候)

1 after() / before()

这个无需多说了,上面的图就是个例子,就是在当前对象的前面或者后面插入内容,被插入的内容是当然对象的兄弟节点

2 change()

和click()或者hover()一样,它也是个事件处理器,这个change事件作用于 textarea, text input, select, 当目标元素的值改变的时候触发该事件。它不同于对象失去焦点时触发的事件foucusOut()和blur()。

change()事件对于客户端验证的任务最适合不过了,因为你不需要在输入字段值没有变化的时候重新验证字段。

$(document).ready(
    function() {
$('input[type=text]').change(function () {
    switch (this.id) {
/* some validation code here */
case "name":
alert(this.value+"....");
break;
default:
alert(this.id);
break;
    }
});
    });

// HTML Form
Name: <input id="name" type="text"/>

3 Context

在jQuery中,使用选择器,其实默认作用于document这个上下文上

了解了这一点话,Context就简单了,它是一个属性兼参数

作为属性:每个jQuery对象,都有个context属性

作为参数:当使用jQuery选择器时,可以使用第二个参数context,细化了查询的范围

用Firebug试验一下就明白了!

4 data() / removeData()

在元素上(确切的说应该是jQuery对象)上存储数据的一种办法

5 queue() / dequeue()

用在jQuery动画效果上,可以方便的增加或者移除一个特效

6 delay()

暂停一段时间的动画效果

7 bind(), unbind(),live(), and die()

以前的文章说过了,不废话了

8 eq()

用数组下标的方式取得一组元素中的某一个

9 get()

取得jQuery元素对应的Dom对象格式

10 grep()

这个我挺喜欢,第二个参数是个用于过滤的回调函数,很容易懂的

 var nums = '1,2,3,4,5,6,7,8,9,10'.split(',');

 nums = $.grep(nums, function(num, index) {
   // num = the current value for the item in the array
   // index = the index of the item in the array
   return num > 5; // returns a boolean
 });

 console.log(nums) // 6,7,8,9,10

11 Pseudo-Selectors

$(':animated'); // returns all elements currently animating
$(':contains(me)'); // returns all elements with the text 'me'
$(':empty'); // returns all elements with no child nodes or text
$(':parent'); // returns all elements with child nodes or text
$('li:even'); // returns all even-index elements (in this case, <li>s)
$('li:odd'); // can you guess?
$(':header'); // returns all h1 - h6s.
$('li:gt(4)'); // returns all elements with an (zero-based) index greater than the given number
$('li:lt(4)'); // returns all element with an index less than the given number
$(':only-child'); // returns all . . . well, it should be obvious

12 isArray() / isEmptyObject() / isFunction() / isPlainObject()

 $.isArray([1, 2, 3]); // returns true
 $.isEmptyObject({}); // returns true
 $.isFunction(function () { /****/ }); // returns true

 function Person(name) {
     this.name = name
     return this;
 }
 $.isPlainObject({})); // returns true
 $.isPlainObject(new Object()); // returns true
 $.isPlainObject(new Person()); // returns false

13 makeArray()

使用jQuery选择器返回的都是jQuery对象,所以$.makeArray()变得挺实用的

    var ps = $('p');
    $.isArray(ps); //returns false;
    ps = $.makeArray(ps);
    $.isArray(ps); // returns true;

14 map()

上面firebug的贴图中延伸过了,自己试试看吧

15 parseJSON()

把JSON格式的字符串结果解析成JSON对象

16 proxy()

$.proxy() 解决了函数调用的上下文问题

 var person = {
   name : "Andrew",
   meet : function () {
     alert('Hi! My name is ' + this.name);
   }
 };
 person.meet();   // 正常工作
 $('#test').click(person.meet); // 返回 Hi! My name is undefined 或者 Hi! My name is 空

$('#test').click($.proxy(person.meet, person));
// we could also do $.proxy(person, "meet")

17 replaceAll() / replaceWith()

用来替换内容的,replaceWith更顺脑

<div class="error1">error section 1</div>
<br/>
<div class="error2">error section 2</div>
<br/>
$('<span class=fixed>The error has been corrected</span>').replaceAll('.error1');
"$('.error2').replaceWith('<span class=fixed>The error has been corrected</span>');

18 serialize() / serializeArray()

看例子吧,易懂

 <form>
     <input type="text" name="name" value="John Doe" />
     <input type="text" name="url" value="http://www.example.com" />
 </form>
console.log($('form').serialize());​​​ // logs : name=John+Doe&url=http%3A%2F%2Fwww.example.com
console.log($('form').serializeArray());
// logs : [{ name : 'name', value : 'John Doe'} , { name : 'url', value : 'http://www.example.com' } ]

19 siblings()

返回所有兄弟节点

20 wrap() / wrapAll() / wrapInner()

在作用对象的外面,周围,或者里面,包一层东西

Conclusion

好好玩这些个方法吧!

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>