常州市物业管理规则:JQuery简单学习(2)——JQuery语法

来源:百度文库 编辑:偶看新闻 时间:2024/04/30 01:57:31

JQuery简单学习(2)——JQuery语法

文章分类:Web前端

 

通过 jQuery,您可以选取(查询,query) HTML 元素,并对它们执行“操作”(actions)。

————————————————————jQuery 语法jQuery 语法是为 HTML 元素的选取编制,可以对元素执行某些操作。
基础语法是:$(selector).action()
美元符号定义 jQuery 选择符(selector)“查询”和“查找” HTML 元素 jQuery action() 执行对元素的操作 实例$(this).hide() - 隐藏当前元素
$("p").hide() - 隐藏所有段落
$("p.test").hide() - 隐藏所有 class="test" 的段落
$("#test").hide() - 隐藏所有 id="test" 的元素
提示:jQuery 使用的语法是 XPath 与 CSS 选择器语法的组合。在本教程接下来的章节,您将学习到更多有关选择器的语法。————————————————————jQuery 语法实例$(this).hide() Js代码
  1.   
  2.   
  3. "text/javascript" src="/jquery/jquery.js">   
  4. "text/javascript">   
  5. $(document).ready(function(){   
  6.   $("button").click(function(){   
  7.   $(this).hide();   
  8. });   
  9. });   
  10.   
  11.   
  12.   
  13.   
  14. "button">Click me   
  15.   
  16.   
  17.   
 演示 jQuery hide() 函数,隐藏当前的 HTML 元素。 
$("#test").hide() Js代码
  1.   
  2.   
  3. "text/javascript" src="/jquery/jquery.js">   
  4. "text/javascript">   
  5. $(document).ready(function(){   
  6.   $("button").click(function(){   
  7.     $("#test").hide();   
  8.   });   
  9. });   
  10.   
  11.   
  12.   
  13.   
  14. This is a heading

      
  15. This is a paragraph.

      
  16. "test">This is another paragraph.

      
  17. "button">Click me   
  18.   
  19.   
  20.   
 演示 jQuery hide() 函数,隐藏 id="test" 的元素。 
$("p").hide() Js代码
  1.   
  2.   
  3. "text/javascript" src="/jquery/jquery.js">   
  4. "text/javascript">   
  5. $(document).ready(function(){   
  6. $("button").click(function(){   
  7. $("p").hide();   
  8. });   
  9. });   
  10.   
  11.   
  12.   
  13.   
  14. This is a heading

      
  15. This is a paragraph.

      
  16. This is another paragraph.

      
  17. "button">Click me   
  18.   
  19.    
 演示 jQuery hide() 函数,隐藏所有

元素。 
$(".test").hide() Js代码

  1.   
  2.   
  3. "text/javascript" src="/jquery/jquery.js">   
  4. "text/javascript">   
  5. $(document).ready(function()   
  6. {   
  7.   $("button").click(function()   
  8.   {   
  9.   $(".test").hide();   
  10.   });   
  11. });   
  12.   
  13.   
  14.   
  15.   
  16. class="test">This is a heading   
  17. class="test">This is a paragraph.

      
  18. This is another paragraph.

      
  19. "button">Click me   
  20.   
  21.   
  22.   
 演示 jQuery hide() 函数,隐藏所有 class="test" 的元素。 ————————————————————文档就绪函数您也许已经注意到在我们的实例中的所有 jQuery 函数位于一个 document ready 函数中:Js代码
  1. $(document).ready(function(){   
  2.   
  3. --- jQuery functions go here ----   
  4.   
  5. });  
 这是为了防止文档在完全加载(就绪)之前运行 jQuery 代码。
下面是两种假如文档完全加载之前运行函数的话,操作失败的情况:
  • 试图隐藏一个不存在的元素。 
  • 获得未完全加载的图像的大小。