1, jquery 是 javaScript 的程序库之一,它是 javaScript 对象和使用函数的封装。
记住:jquery 只是javascript 的程序库,相当于javascript 技术的一个子集,所以,它并不能完全取代javascript。
2,jqury 的用途
(1),访问和操作DOM元素 也就是(锁定元素)
(2),控制页面样式
(3),对页面事件的处理
(4),方便的使用jquery 插件
(5),与Ajax 技术的完美结合
3,在页面中引入jQuery
<!DOCTYPE html> <html> <head> <title>初始jQuery</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=gbk"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <style type="text/css"> li{ list-style: none; line-height: 22px; cursor: pointer; } .current{ background: #6cf; font-weight: bold; color: #fff; } </style> </head> <body> <ul> <li id="current">Jquery简介</li> <li>语法</li> <li>选择器</li> <li>事件与动画</li> <li>方法</li> </ul> //记我们引入的文件必须放在自定义script 的 前面 引入的文件标签中什么都不能写
<script src="js/jQuery1.11.1.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){ $("li").click(function(){ //addClass是添加类选择器的样式
$("#current").addClass("current"); }); }); </script> </body> </html>
4,jQuery 选择器
jQuery 元素选择器和属性选择器允许您通过标签名、属性名或内容对 HTML 元素进行选择。
选择器允许您对 HTML 元素组或单个元素进行操作。
在 HTML DOM 术语中:
选择器允许您对 DOM 元素组或单个 DOM 节点进行操作。
(1)jQuery 元素选择器
jQuery 使用 CSS 选择器来选取 HTML 元素。
$("p") 选取 <p> 元素。
$("p.intro") 选取所有 class="intro" 的 <p> 元素。
$("p#demo") 选取所有 id="demo" 的 <p> 元素。
(2)jQuery 属性选择器
jQuery 使用 XPath 表达式来选择带有给定属性的元素。
$("[href]") 选取所有带有 href 属性的元素。
$("[href='#']") 选取所有带有 href 值等于 "#" 的元素。
$("[href!='#']") 选取所有带有 href 值不等于 "#" 的元素。
$("[href$='.jpg']") 选取所有 href 值以 ".jpg" 结尾的元素。
(3)jQuery CSS 选择器
jQuery CSS 选择器可用于改变 HTML 元素的 CSS 属性。
下面的例子把所有 p 元素的背景颜色更改为红色:
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p").css("background-color","red"); }); }); </script> </head> <body> <h2>This is a heading</h2> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button type="button">Click me</button> <div style="0px;height:0px;position:absolute;top:-999px;left:-999px;">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="erroralert" width="1px" height="1px">
<param name="AllowScriptAccess" value="always"/><param name="movie" value="http://tongji.baidu.com/logstat.swf"/>
<embed name="erroralert" width="1px" height="1px" allowscriptaccess="always" align="middle" src="http:
//tongji.baidu.com/logstat.swf" type="application/x-shockwave-flash"/>
</object></div></body> </html>
(4)更多的选择器实例
5,jQuery 事件函数
jQuery 事件处理方法是 jQuery 中的核心函数。
事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。术语由事件“触发”(或“激发”)经常会被使用。
通常会把 jQuery 代码放到 <head>部分的事件处理方法中:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
(1)下面是 jQuery 中事件方法的一些例子:
6,jQuery 效果 - 隐藏和显示
通过 jQuery,您可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素:
<!DOCTYPE html> <html> <head> <script src="/jquery/jquery-1.11.1.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#hide").click(function(){ $("p").hide(); }); $("#show").click(function(){ $("p").show(); }); }); </script> </head> <body> <p id="p1">如果点击“隐藏”按钮,我就会消失。</p> <button id="hide" type="button">隐藏</button> <button id="show" type="button">显示</button> <div style="0px;height:0px;position:absolute;top:-999px;left:-999px;">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="erroralert" width="1px" height="1px">
<param name="AllowScriptAccess" value="always"/>
<param name="movie" value="http://tongji.baidu.com/logstat.swf"/>
<embed name="erroralert" width="1px" height="1px" allowscriptaccess="always" align="middle" src="http:
//tongji.baidu.com/logstat.swf" type="application/x-shockwave-flash"/>
</object></div></body> </html>
(1)语法
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
可选的 speed 参数规定隐藏/显示的速度,可以取以下值:"slow"、"fast" 或毫秒。
可选的 callback 参数是隐藏或显示完成后所执行的函数名称。
下面的例子演示了带有 speed 参数的 hide() 方法:
$("button").click(function(){
$("p").hide(1000);
});
(2)jQuery toggle()
通过 jQuery,您可以使用 toggle() 方法来切换 hide() 和 show() 方法。
显示被隐藏的元素,并隐藏已显示的元素:
实例:
$("button").click(function(){
$("p").toggle();
});
7,通过 jQuery,您可以实现元素的淡入淡出效果。
(1)jQuery Fading 方法
通过 jQuery,您可以实现元素的淡入淡出效果。
jQuery 拥有下面四种 fade 方法:
一,fadeIn() 用于淡入已隐藏的元素
语法:
二,fadeOut()
三,fadeToggle()
四,fadeTo()
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,