通过 jQuery,您可以选取(查询,query) HTML 元素,并对它们执行“操作”(actions)。
jQuery 语法是为 HTML 元素的选取编制的,可以对元素执行某些操作。基础语法如下:
$(selector).action()解释:
<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 元素。
<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" 的元素。
<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> 元素。
<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" 的元素。
$(document).ready(function(){ // jQuery functions go here });这是为了防止文档在完全加载(就绪)之前运行 jQuery 代码。
评论