jQuery

 1 /**
 2 * 把所有 jQuery 代码置于事件处理函数中
 3 * 把所有事件处理函数置于文档就绪事件处理器中
 4 * 把 jQuery 代码置于单独的 .js 文件中
 5 * 如果存在jQuery 名称冲突,用var jq=jQuery.noConflict(),使用自己的名称(比如 jq)来代替 $ 符号。
 6 */
 7 
 8  
 9 
10 //ready();hide();click();这些都是jquery的事件函数
11 //fadeToggle(speed,callback)显示被隐藏的元素,并隐藏已显示的元素:speed:"slow"、"fast" 或毫秒。
12 //fadeTo(speed,opacity,callback)允许渐变为给定的不透明度
13 // jQuery slideToggle(speed,callback)
14 //animate(); 注意队列功能,依次执行
15 
16 $("button").click(function(){
17   var div=$("div");
18   div.animate({height:'300px',opacity:'0.4'},"slow");
19   div.animate({width:'300px',opacity:'0.8'},"slow");
20   div.animate({height:'100px',opacity:'0.4'},"slow");
21   div.animate({width:'100px',opacity:'0.8'},"slow");
22   div.animate({fontSize:'3em'},"slow");
23 });
24 
25 
26 //callback
27 $("p").hide(1000,function(){
28   alert("The paragraph is now hidden");
29 });
30 
31 
32 //jQuery 方法链接
33 $("#p1").css("color","red")
34   .slideUp(2000)
35   .slideDown(2000);
36 
37 
38 
39 var EBTitle = document.getElementById("EBTitle");
40 var EBcontent = document.getElementById("EBcontent");
41 $(document).ready( //$(document).ready让这个方法在所有元素加载完之后才运行
42 function onclick (place) { //这个地方还是要用原生,因为要代参数进去
43   switch (place){
44     case "HongKong":
45       EBTitle.innerHTML = HongKongA;
46       EBcontent.innerHTML = HongKongB;
47 
48       var txt1="<b>I </b>"; // 以 HTML 创建新元素
49       var txt2=$("<i></i>").text("love "); // 通过 jQuery 创建新元素
50       var txt3=document.createElement("big"); // 通过 DOM 创建新元素
51       txt3.innerHTML="jQuery!";
52       $("EBcontent").append(txt3);
53     break;
54   }
55 })

 






***jQuery DOM 操作

获得内容
* text() - 设置或返回所选元素的文本内容

1 $("#btn1").click(function(){
2   alert("Text: " + $("#test").text());
3   $("#test1").text("Hello world!");
4 });

 

 

* html() - 设置或返回所选元素的内容(包括 HTML 标记)

1 $("#btn2").click(function(){
2   alert("HTML: " + $("#test").html());
3   $("#test1").html("Hello world!");
4 });

 

* val() - 设置或返回表单字段的值

1 $("#btn1").click(function(){
2   alert("Value: " + $("#test").val());
3   $("#test1").val("Hello world!");
4 });

 

* attr() - 方法用于获取属性值。

1 $("#btn1").click(function(){
2   alert("attr: " + $("#test").attr("href",回调函数));
3   "href" : "http://www.w3school.com.cn/jquery",
4   "title" : "W3School jQuery Tutorial"
5 });

 

 

添加新的 HTML 内容

* append() - 在被选元素的结尾插入内容

1 $("p").append("Some appended text.");

 



* prepend() - 在被选元素的开头插入内容

1 $("p").prepend("Some prepended text.");

 


* after() - 在被选元素之后插入内容

1 $("img").after("Some text after");

 


* before() - 在被选元素之前插入内容

1 $("img").before("Some text before");

 





删除元素/内容



* remove() - 删除被选元素(及其子元素)

1 $("#div1").remove();

 



* empty() - 从被选元素中删除子元素

1 $("#div1").empty();

 





jQuery 操作 CSS

* addClass() - 向被选元素添加一个或多个类

1 $("button").click(function(){
2   $("h1,h2,p").addClass("blue");
3   $("div").addClass("important");
4   $("#div1").addClass("important blue");
5 });

 

* removeClass() - 从被选元素删除一个或多个类

1 $("button").click(function(){
2   $("h1,h2,p").removeClass("blue");
3 });

 

* toggleClass() - 对被选元素进行添加/删除类的切换操作

1 $("button").click(function(){
2   $("h1,h2,p").toggleClass("blue");
3 });

 

* css() - 设置或返回样式属性

1 $("p").css("background-color"); 
2 $("p").css("background-color","yellow");
3 $("p").css({"background-color":"yellow","font-size":"200%"});

 





jQuery 尺寸 方法
* width() - 方法设置或返回元素的宽度(不包括内边距、边框或外边距)
* height() - 方法设置或返回元素的高度(不包括内边距、边框或外边距)。

1 $("button").click(function(){
2   var txt="";
3   txt+="Width: " + $("#div1").width() + "</br>";
4   txt+="Height: " + $("#div1").height();
5   $("#div1").html(txt);
6 });

 

* innerWidth() - innerWidth() 方法返回元素的宽度(包括内边距)。
* innerHeight() - innerHeight() 方法返回元素的高度(包括内边距)。

1 $("button").click(function(){
2   var txt="";
3   txt+="Inner width: " + $("#div1").innerWidth() + "</br>";
4   txt+="Inner height: " + $("#div1").innerHeight();
5   $("#div1").html(txt);
6 });

 



* outerWidth() - 方法返回元素的宽度(包括内边距和边框)。
* outerHeight() - 方法返回元素的高度(包括内边距和边框)。

1 $("button").click(function(){
2   var txt="";
3   txt+="Outer width: " + $("#div1").outerWidth() + "</br>";
4   txt+="Outer height: " + $("#div1").outerHeight();
5   $("#div1").html(txt);
6 });

 

* outerWidth(true) 方法返回元素的宽度(包括内边距、边框和外边距)。
* outerHeight(true) 方法返回元素的高度(包括内边距、边框和外边距)。

1 $("button").click(function(){
2   var txt="";
3   txt+="Outer width (+margin): " + $("#div1").outerWidth(true) + "</br>";
4   txt+="Outer height (+margin): " + $("#div1").outerHeight(true);
5   $("#div1").html(txt);
6 });

 



* 返回文档(HTML 文档)和窗口(浏览器视口)的宽度和高度:

1 $("button").click(function(){
2   var txt="";
3   txt+="Document width/height: " + $(document).width();
4   txt+="x" + $(document).height() + "\n";
5   txt+="Window width/height: " + $(window).width();
6   txt+="x" + $(window).height();
7   alert(txt);
8 });

 





* 设置指定的 <div> 元素的宽度和高度:

1 $("button").click(function(){
2   $("#div1").width(500).height(500);
3 });

 






向上遍历 DOM 树
* parent() - 方法返回被选元素的直接父元素。

1 $(document).ready(function(){
2   $("span").parent();
3 });

 

* parents() - 方法返回被选元素的所有祖先元素,它一路向上直到文档的根元素 (<html>)。

1 $(document).ready(function(){
2   $("span").parents();
3 });

 

1 $(document).ready(function(){
2   $("span").parents("ul");
3 });

 

* parentsUntil() - 方法返回介于两个给定元素之间的所有祖先元素。

1 $(document).ready(function(){
2   $("span").parentsUntil("div");
3 });

 




向下遍历 DOM 树
* children() - 方法返回被选元素的所有直接子元素。

1 $(document).ready(function(){
2   $("div").children("p.1");
3 });

 

* find() - 方法返回被选元素的后代元素,一路向下直到最后一个后代。

1 $(document).ready(function(){
2   $("div").find("span");
3 });

 

 

在 DOM 树中水平遍历
* siblings() - 方法返回被选元素的所有同胞元素。

1 $(document).ready(function(){
2   $("h2").siblings("p");
3 });

 

* next() - 方法返回被选元素的下一个同胞元素。

1 $(document).ready(function(){
2   $("h2").next("p");
3 });

 

* nextAll() - 方法返回被选元素的所有跟随的同胞元素。

1 $(document).ready(function(){
2   $("h2").nextAll("p");
3 });

 

* nextUntil() - 方法返回介于两个给定参数之间的所有跟随的同胞元素。

1 $(document).ready(function(){
2   $("h2").nextUntil("h6");
3 });

 

* prev() * prevAll() * prevUntil()
方法的工作方式与上面的方法类似,只不过方向相反而已:它们返回的是前面的同胞元素(在 DOM 树中沿着同胞元素向后遍历,而不是向前)。




缩写搜索元素的范围

* first() - 方法返回被选元素的首个元素。

1 $(document).ready(function(){
2   $("div p").first();
3 });

 

* last() - 方法返回被选元素的最后一个元素。

1 $(document).ready(function(){
2   $("div p").last();
3 });

 

* eq() - 方法返回被选元素中带有指定索引号的元素。

1 $(document).ready(function(){
2   $("p").eq(1);
3 });

 

* filter() - 方法允许您规定一个标准。不匹配这个标准的元素会被从集合中删除,匹配的元素会被返回。

1 $(document).ready(function(){
2   $("p").filter(".intro");
3 });

 

* not() - 方法返回不匹配标准的所有元素。

1 $(document).ready(function(){
2   $("p").not(".intro");
3 });

 

 

AJAX
jQuery load() 方法

* jQuery load() - 方法是简单但强大的 AJAX 方法。从服务器加载数据,并把返回的数据放入被选元素中。
$(selector).load(URL,data,callback);

/*
必需的 URL 参数规定您希望加载的 URL。

可选的 data 参数规定与请求一同发送的查询字符串键/值对集合。

可选的 callback 参数是 load() 方法完成后所执行的函数名称。
*/

可以把 jQuery 选择器添加到 URL 参数。下面的例子把 "demo_test.txt" 文件中 id="p1" 的元素的内容,加载到指定的 <div> 元素中:
$("#div1").load("demo_test.txt #p1");



可选的 callback 参数规定当 load() 方法完成后所要允许的回调函数。回调函数可以设置不同的参数:
* responseTxt - 包含调用成功时的结果内容
* statusTXT - 包含调用的状态
* xhr - 包含 XMLHttpRequest 对象

1 $("button").click(function(){
2   $("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
3     if(statusTxt=="success")
4       alert("外部内容加载成功!");
5     if(statusTxt=="error")
6       alert("Error: "+xhr.status+": "+xhr.statusText);
7   });
8 });

 





AJAX get() 和 post() 方法
两种在客户端和服务器端进行请求-响应的常用方法
GET 基本上用于从服务器获得(取回)数据。注释:GET 方法可能返回缓存数据。
POST 也可用于从服务器获取数据。不过,POST 方法不会缓存数据,并且常用于连同请求一起发送数据。

* GET - 从指定的资源请求数据
* POST - 向指定的资源提交要处理的数据


jQuery $.get() 方法
* $.get() - 方法通过 HTTP GET 请求从服务器上请求数据。

 1 /*
 2 $.get(URL,callback);
 3 $.get() 的第一个参数是我们希望请求的 URL("demo_test.asp")。第二个参数是回调函数。第一个回调参数存有被请求页面的内容,第二个回调参数存有请求的状态。
 4 */
 5 $("button").click(function(){
 6   $.get("demo_test.asp",function(data,status){
 7     alert("Data: " + data + "\nStatus: " + status);
 8   });
 9 });
10 
11 /*
12 ASP 文件 ("demo_test.asp") 类似这样:
13 
14 <%
15 response.write("This is some text from an external ASP file.")
16 %>
17 */

 




jQuery $.post() 方法
* $.post() - 方法通过 HTTP POST 请求从服务器上请求数据。

 1 $.post(URL,data,callback);
 2 
 3 /*
 4 必需的 URL 参数规定您希望请求的 URL。
 5 
 6 可选的 data 参数规定连同请求发送的数据。
 7 
 8 可选的 callback 参数是请求成功后所执行的函数名。
 9 */
10 
11 $("button").click(function(){
12 $.post("demo_test_post.asp",
13 {
14   name:"Donald Duck",
15   city:"Duckburg"
16 },
17 function(data,status){
18   alert("Data: " + data + "\nStatus: " + status);
19 });
20 });
21 
22 /*
23 这个 ASP 文件 ("demo_test_post.asp") 类似这样:
24 
25 <%
26 dim fname,city
27 fname=Request.Form("name")
28 city=Request.Form("city")
29 Response.Write("Dear " & fname & ". ")
30 Response.Write("Hope you live well in " & city & ".")
31 %>
32 */

 




jQuery - noConflict() 方法
* noConflict() - 方法会释放会 $ 标识符的控制,这样其他脚本就可以使用它了。

1 var jq = $.noConflict();
2 jq(document).ready(function(){
3     jq("button").click(function(){
4     jq("p").text("jQuery 仍在运行!");
5   });
6 });

 



如果你的 jQuery 代码块使用 $ 简写,并且您不愿意改变这个快捷方式,那么您可以把 $ 符号作为变量传递给 ready 方法。这样就可以在函数内使用 $ 符号了 - 而在函数外,依旧不得不使用 "jQuery":

1 $.noConflict();
2 jQuery(document).ready(function($){
3     $("button").click(function(){
4     $("p").text("jQuery 仍在运行!");
5   });
6 });

 























































posted @ 2017-12-17 21:44  梁古Lc  阅读(150)  评论(0)    收藏  举报