• jQuery 教程


    jQuery 是一个 JavaScript 库。

    jQuery 极大地简化了 JavaScript 编程。

    jQuery 是一个“写的更少,但做的更多”的轻量级 JavaScript 库。

    基本上,您将学习到如何选取 HTML 元素,以及如何对它们执行类似隐藏、移动以及操作其内容等任务。

    学习 jQuery 之前,您应该对以下知识有基本的了解:

    • HTML
    • CSS
    • JavaScript

    Query 库 - 特性

    jQuery 是一个 JavaScript 函数库。

    jQuery 库包含以下特性:

    • HTML 元素选取
    • HTML 元素操作
    • CSS 操作
    • HTML 事件函数
    • JavaScript 特效和动画
    • HTML DOM 遍历和修改
    • AJAX
    • Utilities

    实例:点击让文字消失

    <!DOCTYPE html>
    <html>
    <head>
    <script src="/jquery/jquery-1.11.1.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("p").click(function(){
        $(this).hide();
      });
    });
    </script>
    </head>
    <body>
    <p>如果您点击我,我会消失。</p>
    <p>点击我,我会消失。</p>
    <p>也要点击我哦。</p>
    </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(){
      $(this).hide();
    });
    });
    </script>
    </head>

    <body>
    <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(){
        $("#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() 函数,隐藏所有 <p> 元素。

    <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() 函数,隐藏所有 class="test" 的元素。

    <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 语法

    jQuery 语法是为 HTML 元素的选取编制的,可以对元素执行某些操作。

    基础语法是:$(selector).action()

    • 美元符号定义 jQuery
    • 选择符(selector)“查询”和“查找” HTML 元素
    • jQuery 的 action() 执行对元素的操作

    示例

    $(this).hide() - 隐藏当前元素

    $("p").hide() - 隐藏所有段落

    $(".test").hide() - 隐藏所有 class="test" 的所有元素

    $("#test").hide() - 隐藏所有 id="test" 的元素

    提示:jQuery 使用的语法是 XPath 与 CSS 选择器语法的组合。在本教程接下来的章节,您将学习到更多有关选择器的语法。

    文档就绪函数

    您也许已经注意到在我们的实例中的所有 jQuery 函数位于一个 document ready 函数中:

    $(document).ready(function(){
    
    --- jQuery functions go here ----
    
    });
    

    这是为了防止文档在完全加载(就绪)之前运行 jQuery 代码。

    如果在文档没有完全加载之前就运行函数,操作可能失败。下面是两个具体的例子:

    • 试图隐藏一个不存在的元素
    • 获得未完全加载的图像的大小

    jQuery 选择器

    选择器实例选取
    * $("*") 所有元素
    #id $("#lastname") id="lastname" 的元素
    .class $(".intro") 所有 class="intro" 的元素
    element $("p") 所有 <p> 元素
    .class.class $(".intro.demo") 所有 class="intro" 且 class="demo" 的元素
         
    :first $("p:first") 第一个 <p> 元素
    :last $("p:last") 最后一个 <p> 元素
    :even $("tr:even") 所有偶数 <tr> 元素
    :odd $("tr:odd") 所有奇数 <tr> 元素
         
    :eq(index) $("ul li:eq(3)") 列表中的第四个元素(index 从 0 开始)
    :gt(no) $("ul li:gt(3)") 列出 index 大于 3 的元素
    :lt(no) $("ul li:lt(3)") 列出 index 小于 3 的元素
    :not(selector) $("input:not(:empty)") 所有不为空的 input 元素
         
    :header $(":header") 所有标题元素 <h1> - <h6>
    :animated   所有动画元素
         
    :contains(text) $(":contains('W3School')") 包含指定字符串的所有元素
    :empty $(":empty") 无子(元素)节点的所有元素
    :hidden $("p:hidden") 所有隐藏的 <p> 元素
    :visible $("table:visible") 所有可见的表格
         
    s1,s2,s3 $("th,td,.intro") 所有带有匹配选择的元素
         
    [attribute] $("[href]") 所有带有 href 属性的元素
    [attribute=value] $("[href='#']") 所有 href 属性的值等于 "#" 的元素
    [attribute!=value] $("[href!='#']") 所有 href 属性的值不等于 "#" 的元素
    [attribute$=value] $("[href$='.jpg']") 所有 href 属性的值包含以 ".jpg" 结尾的元素
         
    :input $(":input") 所有 <input> 元素
    :text $(":text") 所有 type="text" 的 <input> 元素
    :password $(":password") 所有 type="password" 的 <input> 元素
    :radio $(":radio") 所有 type="radio" 的 <input> 元素
    :checkbox $(":checkbox") 所有 type="checkbox" 的 <input> 元素
    :submit $(":submit") 所有 type="submit" 的 <input> 元素
    :reset $(":reset") 所有 type="reset" 的 <input> 元素
    :button $(":button") 所有 type="button" 的 <input> 元素
    :image $(":image") 所有 type="image" 的 <input> 元素
    :file $(":file") 所有 type="file" 的 <input> 元素
         
    :enabled $(":enabled") 所有激活的 input 元素
    :disabled $(":disabled") 所有禁用的 input 元素
    :selected $(":selected") 所有被选取的 input 元素
    :checked $(":checked") 所有被选中的 input 元素
  • 相关阅读:
    sql server 2005 时间转化获取年,有一个时间如20090715,现在要分别查出年、月、日,
    在服务器上使用第三方独立组件对Word/Excel进行编程
    Session的配置
    sql 2005判断某个表或某个表中的列是否存在
    GridView中FooterTemplate模板下内容不显示
    asp.net 按钮单击事件问题(自动弹出新窗口)
    NameValueCollection详解
    System.Web.HttpException: 类型“TextBox”的控件“ctl02_TextBox2”必须放在具有 runat=server 的窗体标记内。
    SqlBulkCopy批量复制数据
    使用嵌入式关系型SQLite数据库存储数据
  • 原文地址:https://www.cnblogs.com/yuguangblog/p/6014829.html
Copyright © 2020-2023  润新知