Home > Web Front-end > JS Tutorial > An introduction to learning jQuery with example code_jquery

An introduction to learning jQuery with example code_jquery

WBOY
Release: 2016-05-16 18:32:12
Original
812 people have browsed it

程序代码
window.onload = function(){ ... } .
访问HTML文档的元素,必须先载入文档对象模型(DOM)。当window.onload函数执行的时候,说明所有东西已经载入,包括图像和横幅等等。要知道较大的图片下载速度会比较慢,因此用户必须等待大图片下载完毕才能看到window.onload()执行的代码效果,这样就花费了很长的等待时间,这不是我们想要的。
对于此,jquery提供了一个"ready"事件,你可以使用以下的代码片段:
程序代码
$(document).ready(function(){//获取文档对象就绪的时候,不需要等待图片等下载完成。
// 你的代码
});
$(document)意思是说,获取整个网页文档对象(类似的于window.document),$(document).ready意思就是说,获取文档对象就绪的时候。
上面这段代码的意思是检查文档对象直到它能够允许被操作(译者注:这样做比window.onload()函数要快的多,因为只要文档对象载入完成就能够执行代码了,而不需要等待页面中的图片下载是否已经完成)---这是我们想要的。好了 ,其他的也不多说了,我们开始来用jQ写几个简单的例子。

1,demo1: --鼠标点击时的触发
首先,我们尝试鼠标点击超链接时触发某些行为。在ready函数里加入以下代码:
程序代码
$("p").click(function(){//获取所有段落p的对象,为其加上点击事件,需要加在readey中
// 你的代码
});

2,demo2:--增加 CSS Class
另外一个事情就是,一个共同的任务:增加或移除元素的css class,例如:
程序代码
$("a").addClass("test");
$("a").removeClass("test"); //样式的切换可以通过 $("p").toggleClass("selected");

3,demo3:--show( )和html()的使用
$("a").addClass("test").show().html("foo");//jquery方法可以连写
// how( ):显示隐藏的匹配元素。
//html("info"):设置每一个匹配元素的html内容。

4,demo4:--特效hide()
$("a").click(function(){
$(this).hide("slow");//对象慢慢的消失、隐藏
return false; //表示不会跳转,等同js
});



5,demo5:---收缩展开功能
$(document).ready(function(){
$("#head").click(function(){
$("#content").slideToggle("slow",function(){ alert("Hello,cssrain.."); } );
});// slideToggle(speed, callback)高度变化切换可见性,完成后可触发一个回调函数
});// speed "slow", "normal", or "fast" 也可以指定一数值

6,demo6:--appendTo的用法
{$("#head2").click(function()
{$("
").appendTo("#ccc");});}

看这里的变化

//appendTo(): ​​Append all matching elements to another, specified element set, that is, add sub-nodes
//append(): Add sub-nodes to an element


7, demo7:--The table changes color every other row, the mouse slides over the color, and the color changes when clicked.
Code explanation:
I have already put the explanation in the example, so I will not post comments here.
The examples used are: mouseover(), addClass(), mouseout(), removeClass(), click(),
toggleClass(), tr:even and other methods.
In addition, the difference between toggle() and toggleClass() is explained.
In addition, in this example, I used chain operation. You can view the explanation in chain operation.txt.

8, demo8:--toggle() usage:
$("p").toggle()//Switch the visible state of the element, but please note that this affects all p, You can also switch between two methods toggle( Function even, Function odd ).

9, demo9: --hover() usage:
Hover(function over, function out)//Imitate hover events
$("#orderedlist tr").hover(function over ,function out ) //Add

10,demo10:-- $ to all rows of a table. You can also use jQuery instead of
$(document).ready(function(){// Your Code});//You can also use jQuery instead of $
jQuery(document).ready(function(){
jQuery(".").click(function(){jQuery(this).toggleClass ("")})
});//The advantage is that you may use other js libraries that also use $, which may conflict. jQuery instead of $ is a safer way of writing.

Also:
$(document).ready(function(){//your code});//Abbreviation: $(function() {//your code} );



11, demo11:--each—Usage of find
$("#orderedlist").find("li").each(function(i) { })
//find("li") finds all li elements, and each() executes the method on each object in the collection
//each(Function function fn) to match each The element is used as a context to execute a function

12, demo12:--parents() usage:
$(this).parents("p").addClass("highlight");// The nearest label
this.parent()//refers to the parent node of the object

13, demo13:--Usage of load():
$("#feeds") .load("FAQ1.html",function() { alert("load is done");}

//From the remote one Load HTML from the file and inject it into the DOM

14, demo14: --next usage:
.next()//Get the next sibling node of the object

Package download addressjQuery beginners learning example code set

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template