jQuery Notes (getting started with jQuery, examples of selectors)

本文介绍jQuery的基本用法及高级技巧,包括文档加载完成事件、选择器使用、hover效果实现、表单重置等操作,帮助读者更好地掌握jQuery,提高前端开发效率。

1. document ready event handler (get a feeling about jQuery)

Inside the document ready event handler, we define a function to pop up an alert whenever a link is clicked.

$("a") is a jQuery selector, which selects all "a" elements. $ is an alias for jQuery class, so $() constructs a new object.

JavaScript code can also do the same thing, but we can see js is more neat, we don't have to specify onclick handler for each hyperlink.

 

2. selector example and hover effect

 

orderedlisg: id of an ordered list element inside webpage

li:last selects the last listed item

define two functions for hover handler, one is executed when item is hovered, the other executed when item unhovered

 

Note that in jQuery, we have equivalent things as we have onXXX in JavaScript.

 

Selector reference: http://docs.jquery.com/DOM/Traversing/Selectors

 

3. more things you can do

$("#orderedlist").find("li") is mostly the same as $("#orderedlist li")

$(this).append() is used to append text to the inner html of the selected element.

 

4. reset forms after submitted via ajax

Note that reset() function belongs to form element instead of jquery object, so we cannot simply call $("form").reset() to reset all forms. This represents the actually html element.

 

For all document elements you want to locate, you can find a way to writing your selector, so feel free to use your imagination.

 

The last example is really amazing, you can write a document ready hanlder as follows

 

Lasted modified on 17 Mar, 2011

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值