1. 程式人生 > >Web開發——JavaScript庫(jQuery 語法 / 選擇器 / 事件)

Web開發——JavaScript庫(jQuery 語法 / 選擇器 / 事件)

gb2312 cto 選擇 sel color 事件處理器 效果 處理程序 http

  通過 jQuery,您可以選取(查詢,query) HTML 元素,並對它們執行“操作”(actions)。

1、jQuery 語法

1.1 jQuery語法舉例

1.1.1 $(this).hide()

  演示 jQuery hide() 函數,隱藏當前的 HTML 元素。

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <meta http-equiv="Content-Type" content="text/html; charset=gb2312"
/> 6 <meta http-equiv="Content-Language" content="zh-cn" /> 7 <title>My test page</title> 8 9 <!--引用jQuery庫,src可以直接指向本地下載的jQery庫--> 10 <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">--> 11 <
script src="jquery-3.3.1.js"></script>
12 <script type="text/javascript"> 13 $(document).ready(function() { 14 $("button").click(function() { 15 $(this).hide(); 16 }); 17 }); 18 </script> 19
</head> 20 21 <body> 22 23 <button type="button">Click me</button> 24 25 </body> 26 </html>

  輸出結果:出現“Click me”按鈕,點擊即消失。

1.1.2 $("#id_test").hide()

  演示 jQuery hide() 函數,隱藏 id="id_test" 的元素。

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 6         <meta http-equiv="Content-Language" content="zh-cn" />
 7         <title>My test page</title>
 8         
 9         <!--引用jQuery庫,src可以直接指向本地下載的jQery庫-->
10         <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">-->
11         <script src="jquery-3.3.1.js"></script>
12         <script type="text/javascript">
13             $(document).ready(function() {
14                 $("button").click(function() {
15                     $("#p1_test").hide();
16                 });
17             });
18         </script>
19     </head>
20     
21     <body>
22     
23         <h2>This is a heading</h2>
24         <p>This is a paragraph.</p>
25         <p id="p1_test">This is another paragraph.</p>
26         <button type="button">Click me</button>
27 
28     </body>
29 </html>

  輸出結果:點擊按鈕,“This is another paragraph.”會消失。

This is a heading

This is a paragraph.

This is another paragraph.

1.1.3 $("p").hide()

  演示 jQuery hide() 函數,隱藏所有 <p> 元素。

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 6         <meta http-equiv="Content-Language" content="zh-cn" />
 7         <title>My test page</title>
 8         
 9         <!--引用jQuery庫,src可以直接指向本地下載的jQery庫-->
10         <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">-->
11         <script src="jquery-3.3.1.js"></script>
12         <script type="text/javascript">
13             $(document).ready(function() {
14                 $("button").click(function() {
15                     $("p").hide();
16                 });
17             });
18         </script>
19     </head>
20     
21     <body>
22     
23         <h2>This is a heading</h2>
24         <p>This is a paragraph.</p>
25         <p>This is another paragraph.</p>
26         <button type="button">Click me</button>
27 
28     </body>
29 </html>

  輸出結果:點擊按鈕後,“This is a paragraph.”和“This is another paragraph.”都會消失。

This is a heading

This is a paragraph.

This is another paragraph.

1.1.4 $(".class_test").hide()

  演示 jQuery hide() 函數,隱藏所有 class="class_test" 的元素。

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 6         <meta http-equiv="Content-Language" content="zh-cn" />
 7         <title>My test page</title>
 8         
 9         <!--引用jQuery庫,src可以直接指向本地下載的jQery庫-->
10         <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">-->
11         <script src="jquery-3.3.1.js"></script>
12         <script type="text/javascript">
13             $(document).ready(function() {
14                 $("button").click(function() {
15                     $(".p1_class").hide();
16                 });
17             });
18         </script>
19     </head>
20     
21     <body>
22     
23         <h2>This is a heading</h2>
24         <p class="p1_class">This is a paragraph.</p>
25         <p>This is another paragraph.</p>
26         <button type="button">Click me</button>
27 
28     </body>
29 </html>

  輸出結果:點擊按鈕後,“This is a paragraph.”會消失。

This is a heading

This is a paragraph.

This is another paragraph.

1.2 jQuery 語法

  jQuery 語法是為 HTML 元素的選取編制的,可以對元素執行某些操作。

  基礎語法是:$(selector).action()

  • 美元符號定義 jQuery
  • 選擇符(selector)“查詢”和“查找” HTML 元素
  • jQuery 的 action() 執行對元素的操作

1.3 文檔就緒函數

  您也許已經註意到在我們的實例中的所有 jQuery 函數位於一個 document ready 函數中:

1 $(document).ready(function() {
2     <!--jQuery functions go here-->
3 });

  這是為了防止文檔在完全加載(就緒)之前運行 jQuery 代碼。

  如果在文檔沒有完全加載之前就運行函數,操作可能失敗。下面是兩個具體的例子:

  • 試圖隱藏一個不存在的元素
  • 獲得未完全加載的圖像的大小

2、jQuery選擇器

  在前面的章節中,我們展示了一些有關如何選取 HTML 元素的實例。

  關鍵點是學習 jQuery 選擇器是如何準確地選取您希望應用效果的元素。

  jQuery 元素選擇器和屬性選擇器允許您通過標簽名、屬性名或內容對 HTML 元素進行選擇。

  選擇器允許您對 HTML 元素組或單個元素進行操作。

  在 HTML DOM 術語中:

  選擇器允許您對 DOM 元素組或單個 DOM 節點進行操作。

2.1 jQuery元素選擇器  

  jQuery 使用 CSS 選擇器來選取 HTML 元素。

  1. $("p") 選取 <p> 元素。
  2. $("p.intro") 選取所有 class="intro" 的 <p> 元素。
  3. $(".intro") 選取所有 class="intro" 的所有元素。
  4. $("p#demo") 選取所有 id="demo" 的 <p> 元素。
  5. $("#demo") 選取所有 id="demo" 的所有元素

2.2 jQuery屬性選擇器 

  jQuery 使用 XPath 表達式來選擇帶有給定屬性的元素。

  1. $("[href]") 選取所有帶有 href 屬性的元素。
  2. $("[href=‘#‘]") 選取所有帶有 href 值等於 "#" 的元素。
  3. $("[href!=‘#‘]") 選取所有帶有 href 值不等於 "#" 的元素。
  4. $("[href$=‘.jpg‘]") 選取所有 href 值以 ".jpg" 結尾的元素。

2.3 jQuery CSS選擇器

  jQuery CSS 選擇器可用於改變 HTML 元素的 CSS 屬性。

  下面的例子把所有 p 元素的背景顏色更改為紅色:

1 $("p").css("background-color","red");

2.4 更多的選擇器實例

語法描述
$(this) 當前 HTML 元素
$("p") 所有 <p> 元素
$("p.intro") 所有 class="intro" 的 <p> 元素
$(".intro") 所有 class="intro" 的元素
$("#intro") id="intro" 的元素
$("ul li:first") 每個 <ul> 的第一個 <li> 元素
$("[href$=‘.jpg‘]") 所有帶有以 ".jpg" 結尾的屬性值的 href 屬性
$("div#intro .head") id="intro" 的 <div> 元素中的所有 class="head" 的元素

  如需完整的參考手冊,請訪問我們的 jQuery 選擇器參考手冊。

3、jQuery事件

  jQuery 事件處理方法是 jQuery 中的核心函數。

  事件處理程序指的是當 HTML 中發生某些事件時所調用的方法。術語由事件“觸發”(或“激發”)經常會被使用。

  通常會把 jQuery 代碼放到 <head>部分的事件處理方法中:

  舉例:jQuery語法舉例

  由於 jQuery 是為處理 HTML 事件而特別設計的,那麽當您遵循以下原則時,您的代碼會更恰當且更易維護:

  • 把所有 jQuery 代碼置於事件處理函數中
  • 把所有事件處理函數置於文檔就緒事件處理器中
  • 把 jQuery 代碼置於單獨的 .js 文件中
  • 如果存在名稱沖突,則重命名 jQuery 庫

3.1 jQuery 名稱沖突(如與Prototype都使用到$符號)

  jQuery 使用 $ 符號作為 jQuery 的簡介方式。

  某些其他 JavaScript 庫中的函數(比如 Prototype)同樣使用 $ 符號。

  jQuery 使用名為 noConflict() 的方法來解決該問題。

  var jq=jQuery.noConflict(),幫助您使用自己的名稱(比如 jq)來代替 $ 符號。

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 6         <meta http-equiv="Content-Language" content="zh-cn" />
 7         <title>My test page</title>
 8         
 9         <!--引用jQuery庫,src可以直接指向本地下載的jQery庫-->
10         <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">-->
11         <script src="jquery-3.3.1.js"></script>
12         <script type="text/javascript">
13         
14         <!--方式 1-->
15 <!--        $(document).ready(function() {                    -->
16 <!--                $("button").click(function() {            -->
17 <!--                $("#p1").hide();                        -->
18 <!--            });                                            -->
19 <!--        });                                                -->        
20         
21         <!--方式 2-->
22 <!--        $.noConflict();                                    -->
23 <!--        jQuery(document).ready(function() {                -->        
24 <!--               jQuery("button").click(function() {        -->
25 <!--               jQuery("#p1").hide();                        -->
26 <!--           });                                                -->
27 <!--    });                                                    -->
28 
29         <!--方式 3-->
30             var jq = jQuery.noConflict();
31             jq(document).ready(function() {                    
32                 jq("button").click(function() {            
33                 jq("#p1").hide();                        
34             });                                            
35         });
36         
37         </script>
38     </head>
39     
40     <body>
41     
42         <p id="p1">This is a paragraph</p>
43         <button>Click me</button>
44 
45     </body>
46 </html>

3.2 jQuery事件

  下面是 jQuery 中事件方法的一些例子:

Event 函數綁定函數至
$(document).ready(function) 將函數綁定到文檔的就緒事件(當文檔完成加載時)
$(selector).click(function) 觸發或將函數綁定到被選元素的點擊事件
$(selector).dblclick(function) 觸發或將函數綁定到被選元素的雙擊事件
$(selector).focus(function) 觸發或將函數綁定到被選元素的獲得焦點事件
$(selector).mouseover(function) 觸發或將函數綁定到被選元素的鼠標懸停事件

  如需完整的參考手冊,請訪問 jQuery 事件參考手冊。

Web開發——JavaScript庫(jQuery 語法 / 選擇器 / 事件)