通過(guò) jQuery,您可以選?。ú樵?xún),query) HTML 元素,并對它們執行“操作”(actions)。
————————————————————
jQuery 語(yǔ)法
jQuery 語(yǔ)法是為 HTML 元素的選取編制,可以對元素執行某些操作。
基礎語(yǔ)法是:$(selector).action()
美元符號定義 jQuery
選擇符(selector)“查詢(xún)”和“查找” HTML 元素
jQuery action() 執行對元素的操作
實(shí)例
$(this).hide() - 隱藏當前元素
$("p").hide() - 隱藏所有段落
$("p.test").hide() - 隱藏所有 class="test" 的段落
$("#test").hide() - 隱藏所有 id="test" 的元素
提示:jQuery 使用的語(yǔ)法是 XPath 與 CSS 選擇器語(yǔ)法的組合。在本教程接下來(lái)的章節,您將學(xué)習到更多有關(guān)選擇器的語(yǔ)法。
————————————————————
jQuery 語(yǔ)法實(shí)例
$(this).hide()
- <html>
- <head>
- <script type="text/javascript" src="/jquery/jquery.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- $("button").click(function(){
- $(this).hide();
- });
- });
- </script>
- </head>
-
- <body>
- <button type="button">Click me</button>
- </body>
-
- </html>
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){$("button").click(function(){$(this).hide();});});</script></head><body><button type="button">Click me</button></body></html> 演示 jQuery hide() 函數,隱藏當前的 HTML 元素。
$("#test").hide()
- <html>
- <head>
- <script type="text/javascript" src="/jquery/jquery.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- $("button").click(function(){
- $("#test").hide();
- });
- });
- </script>
- </head>
-
- <body>
- <h2>This is a heading</h2>
- <p>This is a paragraph.</p>
- <p id="test">This is another paragraph.</p>
- <button type="button">Click me</button>
- </body>
-
- </html>
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){$("button").click(function(){$("#test").hide();});});</script></head><body><h2>This is a heading</h2><p>This is a paragraph.</p><p id="test">This is another paragraph.</p><button type="button">Click me</button></body></html> 演示 jQuery hide() 函數,隱藏 id="test" 的元素。
$("p").hide()
- <html>
- <head>
- <script type="text/javascript" src="/jquery/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 type="button">Click me</button>
- </body>
- </html>
<html><head><script type="text/javascript" src="/jquery/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 type="button">Click me</button></body></html> 演示 jQuery hide() 函數,隱藏所有 <p> 元素。
$(".test").hide()
- <html>
- <head>
- <script type="text/javascript" src="/jquery/jquery.js"></script>
- <script type="text/javascript">
- $(document).ready(function()
- {
- $("button").click(function()
- {
- $(".test").hide();
- });
- });
- </script>
- </head>
- <body>
-
- <h2 class="test">This is a heading</h2>
- <p class="test">This is a paragraph.</p>
- <p>This is another paragraph.</p>
- <button type="button">Click me</button>
-
- </body>
- </html>
<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){$("button").click(function(){$(".test").hide();});});</script></head><body><h2 class="test">This is a heading</h2><p class="test">This is a paragraph.</p><p>This is another paragraph.</p><button type="button">Click me</button></body></html> 演示 jQuery hide() 函數,隱藏所有 class="test" 的元素。
————————————————————
文檔就緒函數
您也許已經(jīng)注意到在我們的實(shí)例中的所有 jQuery 函數位于一個(gè) document ready 函數中:
- $(document).ready(function(){
-
- --- jQuery functions go here ----
-
- });
$(document).ready(function(){--- jQuery functions go here ----}); 這是為了防止文檔在完全加載(就緒)之前運行 jQuery 代碼。
下面是兩種假如文檔完全加載之前運行函數的話(huà),操作失敗的情況:
- 試圖隱藏一個(gè)不存在的元素。
- 獲得未完全加載的圖像的大小。