jQuery学习笔记
来源:互联网 发布:安卓手机淘宝网 编辑:程序博客网 时间:2024/06/08 07:01
jQuery是什么?是一个JavaScript库,使用JavaScript语言写的一个函数库,通过调用这个库中的各种函数,简化直接使用JavaScript编写程序,高效快捷简便的对HTML页面进行处理。
jQuery库包括以下功能:
a. HTML元素选取与操作
b. CSS操作
c. HTML事件函数
d. JavaScript的特效与动画
e. HTML DOM的遍历和修改
f. AJAX
既然jQuery是一个JavaScript语言编写的轻量级库,那么在使用时就需要导入这个库,jQuery的库文件可以从官网下载http://jquery.com/download/#Download_jQuery
在使用时,将库文件导入到使用页面中:
<javascript type="text/javascript" src="jquery.js"></javascript>
1、语法
jQuery是为HTML元素的选取与操作编制的,选取元素并对元素操作,所以其语法是这样的:
$(selector).action()
其中,$是jQuery的定义与标示,()是选择器,根据selector进行选择元素,action()是对选择出的元素进行操作。
jQuery的使用部分一般放置在如下位置:
<script type="text/javascript" src="jquery-1.12.2.js"></script><script type="text/javascript">$(document).ready(function(){//使用jQuery的地方});</script>$(document).ready();是对document元素进行选取,在该元素ready操作里执行jQuery的使用。
这是为了防止在文档未完全加载完成之前,对jQuery进行使用。在文档未加载完成之前使用,会导致出错。
看一个简单的例子:jQuery中hide()函数的使用,hide()函数具有隐藏的功能,对选取的元素进行隐藏。
<script type="text/javascript" src="jquery-1.12.2.js"></script><script type="text/javascript">$(document).ready(function(){//为HTML中的button元素的click事件添加函数,//执行哪一个button的click事件时,对该元素的事件执行函数$("button").click(function(){$(this).hide();});});</script></head><body><button type="button">Click me</button><button type="button">Click me</button></body>先为元素的事件添加函数,再在函数中添加对应的操作,$(this)是选取当前的元素。
2、选择器
关键点是如何使用选择器准确的选出希望进行操作的元素。选择器可分为元素选择器、属性选择器,运行通过标签名、属性名以及内容对HTML元素进行选择。
选择器允许对DOM元素组或单个元素节点进行操作。
元素选择器
$("p") 选取所以<p>元素
$("p.intro") 选取所有满足class="intro"的<p>元素
$("p#demo") 选取第一个id="demo"的<p元素>
属性选择器
$("[href]") 选取所有带有href属性的元素
$("[href='#']") 选取所有href属性值等于"#"的元素
$("[href!='#']") 选取所有href属性值不等于"#"的元素
$("[href$='.jpg']") 选取所有href属性值以.jpg结尾的元素
3、事件函数
<script type="text/javascript">//使用其他的标示符代替$,避免冲突var jq= jQuery.noConflict();jq(document).ready(function(){//为HTML中的button元素的click事件添加函数,//执行哪一个button的click事件时,对该元素的事件执行函数jq("button").click(function(){jq(this).hide();});});</script>
自己编写的js库,一般为了方便维护,会写在单独的js文件中,在使用时,像引入jQuery一样引入就可以使用
4、效果
hide(), show()
<html><head><meta charset="utf-8"/><script type="text/javascript" src="jquery-1.3.1.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><p>dsfgfsf</p><button id="hide" type="button">隐藏</button><button id="show" type="button">显示</button></body></html>先根据id选择元素,再对元素添加相应的显示和隐藏事件
5、Callback函数
$(selector).hide(speed,callback)
<html><head><script type="text/javascript" src="jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("p").hide(500, function(){ alert("The paragraph is now hidden"); }); });});</script></head><body><button type="button">Hide</button><p>This is a paragraph with little content.</p></body></html>
6. HTML操作
$(selector).html(content)如,修改所有p元素的内容为"phpStudy"
<html><head><script type="text/javascript" src="jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("p").html("phpStudy"); });});</script></head><body><h2>This is a heading</h2><p>This is a paragraph.</p><p>This is another paragraph.</p><button type="button">请点击这里</button></body></html>还有很多方法,如下:
7. CSS函数
主要是更改元素的渲染效果,方法有:<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","font-size":"200%"}); });});</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></body></html>css(name)的使用,获取样式的属性值,并显示在文本中
<html><head><script type="text/javascript" src="jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("div").click(function(){ $("#result").html($(this).css("background-color")); });});</script></head><body><div style="width:100px;height:100px;background:#ff0000"></div><p id="result">Click in the box</p></body></html>
8. Ajax函数
- 【学习笔记】JQuery学习笔记
- jQuery 学习笔记 + jQuery-UI 学习笔记
- 【jQuery 学习笔记】初识jQuery
- jQuery 学习笔记------jQuery选择器
- jquery学习笔记----初识jquery
- jQuery学习笔记--jQuery Ajax
- jquery学习笔记
- jquery选择器学习笔记
- jQuery学习笔记
- 【jquery】学习笔记
- jQuery学习笔记
- jquery学习笔记
- jquery 学习笔记
- JQuery学习笔记
- Jquery学习笔记
- JQuery学习笔记
- jQuery学习笔记
- jquery学习笔记(二)
- 2014 神奇算式
- delphi7的Application.ProcessMessages作用
- css伪类与伪元素
- Powerful Incantation(HDU 4150)
- 【bzoj1954】【The xor-longest Path】【trie树】
- jQuery学习笔记
- Application.ProcessMessages
- LeetCode 185. Department Top Three Salaries
- android studio 导入一个开源库文件汇总
- Execute & ExecuteGlobal
- R3多线程
- 观察者模式(这个用的太多了)
- 使用extern "C"改善显式调用dll
- 最长回文子串(动态规划和递归)