zoukankan      html  css  js  c++  java
  • jQuery 选择器与事件

    jQuery 选择器

    jQuery 选择器允许您对 HTML 元素组或单个元素进行操作。

    jQuery 选择器基于元素的 id、类、类型、属性、属性值等"查找"(或选择)HTML 元素。 它基于已经存在的 CSS 选择器,除此之外,它还有一些自定义的选择器。

    jQuery 中所有选择器都以美元符号开头:$()。

    元素选择器

    jQuery 元素选择器基于元素名选取元素。

    在页面中选取所有 <p> 元素:

    $("p") 
    

    #id 选择器

    jQuery #id 选择器通过 HTML 元素的 id 属性选取指定的元素。

    页面中元素的 id 应该是唯一的,所以要在页面中选取唯一的元素需要通过 #id 选择器。

    id 选取元素语法如下:

    $("#test")
    

    .class 选择器

    jQuery 类选择器可以通过指定的 class 查找元素。

    语法如下:

    $(".test") 
    
    语法描述
    $("*") 选取所有元素
    $(this) 选取当前 HTML 元素
    $("p.intro") 选取 class 为 intro 的 <p> 元素
    $("p:first") 选取第一个 <p> 元素
    $("ul li:first") 选取第一个 <ul> 元素的第一个 <li> 元素
    $("ul li:first-child") 选取每个 <ul> 元素的第一个 <li> 元素
    $("[href]") 选取带有 href 属性的元素
    $("a[target='_blank']") 选取所有 target 属性值等于 "_blank" 的 <a> 元素
    $("a[target!='_blank']") 选取所有 target 属性值不等于 "_blank" 的 <a> 元素
    $(":button") 选取所有 type="button" 的 <input> 元素 和 <button> 元素
    $("tr:even") 选取偶数位置的 <tr> 元素
    $("tr:odd") 选取奇数位置的 <tr> 元素

    jQuery 事件

    jQuery 是为事件处理特别设计的。

    什么是事件?

    页面对不同访问者的响应叫做事件。事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。

    例如:

    • 在元素上移动鼠标。
    • 选取单选按钮
    • 点击元素

    在事件中经常使用术语"触发"(或"激发")例如: "当您按下按键时触发 keypress 事件"。

    鼠标事件键盘事件表单事件文档/窗口事件
    click keypress submit load
    dblclick keydown change resize
    mouseenter keyup focus scroll
    mouseleave hover blur unload

    jQuery 事件方法语法

    在 jQuery 中,大多数 DOM 事件都有一个等效的 jQuery 方法。

    页面中指定一个点击事件:

    $("p").click();
    

     定义什么时间触发事件。可以通过一个事件函数实现:

    $("p").click(function(){
        // 动作触发后执行的代码!!
    });
    

    常用的 jQuery 事件方法

    $(document).ready()

    $(document).ready() 方法允许我们在文档完全加载完后执行函数。

    click()

    click() 方法是当按钮点击事件被触发时会调用一个函数。

    该函数在用户点击 HTML 元素时执行。

    当点击事件在某个 <p> 元素上触发时,隐藏当前的 <p> 元素:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>mtimeyu</title> 
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("p").click(function(){
        $(this).hide();
      });
    });
    </script>
    </head>
    <body>
    
    <p>如果你点我,我就会消失。</p>
    <p>点我消失!</p>
    <p>点我也消失!</p>
    
    </body>
    </html>
    

     

    dblclick()

    当双击元素时,会发生 dblclick 事件。

    dblclick() 方法触发 dblclick 事件,或规定当发生 dblclick 事件时运行的函数:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>mtimeyu</title> 
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("p").dblclick(function(){
        $(this).hide();
      });
    });
    </script>
    </head>
    <body>
    
    <p>双击鼠标左键的,我就消失。</p>
    <p>双击我消失!</p>
    <p>双击我也消失!</p>
    
    </body>
    </html>
    

     

    mouseenter()

    当鼠标指针穿过元素时,会发生 mouseenter 事件。

    mouseenter() 方法触发 mouseenter 事件,或规定当发生 mouseenter 事件时运行的函数:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>mtimeyu</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("#p1").mouseenter(function(){
        alert('您的鼠标移到了 id="p1" 的元素上!');
      });
    });
    </script>
    </head>
    <body>
    
    <p id="p1">鼠标指针进入此处,会看到弹窗。</p>
    
    </body>
    </html>
    

     

    mouseleave()

    当鼠标指针离开元素时,会发生 mouseleave 事件。

    mouseleave() 方法触发 mouseleave 事件,或规定当发生 mouseleave 事件时运行的函数:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>mtimeyu</title> 
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("#p1").mouseleave(function(){
        alert("再见,您的鼠标离开了该段落。");
      });
    });
    </script>
    </head>
    <body>
    
    <p id="p1">这是一个段落。</p>
    
    </body>
    </html>
    

     

    mousedown()

    当鼠标指针移动到元素上方,并按下鼠标按键时,会发生 mousedown 事件。

    mousedown() 方法触发 mousedown 事件,或规定当发生 mousedown 事件时运行的函数:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>mtimeyu</title> 
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("#p1").mousedown(function(){
        alert("鼠标在该段落上按下!");
      });
    });
    </script>
    </head>
    <body>
    
    <p id="p1">这是一个段落</p>
    
    </body>
    </html>
    

     

    mouseup()

    当在元素上松开鼠标按钮时,会发生 mouseup 事件。

    mouseup() 方法触发 mouseup 事件,或规定当发生 mouseup 事件时运行的函数:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>mtimeyu</title> 
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("#p1").mouseup(function(){
        alert("鼠标在段落上松开。");
      });
    });
    </script>
    </head>
    <body>
    
    <p id="p1">这是一个段落。</p>
    
    </body>
    </html>
    

     

    hover()

    hover()方法用于模拟光标悬停事件。

    当鼠标移动到元素上时,会触发指定的第一个函数(mouseenter);当鼠标移出这个元素时,会触发指定的第二个函数(mouseleave)。

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
        $("#p1").hover(
    		function(){
    			alert("你进入了 p1!");
    		},
    		function(){
    			alert("现在你离开了 p1!");
    		}
        )
    });
    </script>
    </head>
    <body>
    
    <p id="p1">这是一个段落。</p>
    
    </body>
    </html>
    

     

    focus()

    当元素获得焦点时,发生 focus 事件。

    当通过鼠标点击选中元素或通过 tab 键定位到元素时,该元素就会获得焦点。

    focus() 方法触发 focus 事件,或规定当发生 focus 事件时运行的函数:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>mtimeyu</title> 
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("input").focus(function(){
        $(this).css("background-color","#cccccc");
      });
      $("input").blur(function(){
        $(this).css("background-color","#ffffff");
      });
    });
    </script>
    </head>
    <body>
    
    Name: <input type="text" name="fullname"><br>
    Email: <input type="text" name="email">
    
    </body>
    </html>
    

     

    blur()

    当元素失去焦点时,发生 blur 事件。

    blur() 方法触发 blur 事件,或规定当发生 blur 事件时运行的函数:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"> 
    <title>mtimeyu</title> 
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
      $("input").focus(function(){
        $(this).css("background-color","#cccccc");
      });
      $("input").blur(function(){
        $(this).css("background-color","#ffffff");
      });
    });
    </script>
    </head>
    <body>
    
    Name: <input type="text" name="fullname"><br>
    Email: <input type="text" name="email">
    
    </body>
    </html>
    

  • 相关阅读:
    使用FormatterServices 类序列化或反序列化
    HaozesFx(飞信精灵)发布
    EF Provider for Access/ODBC 以及ADO.Net Entity Framework 与Linq to SQL的比较和适用场景:
    Fetion2008 分析 Part3:会话
    Gleaner(个人文档管理)
    This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.
    发一个收取Pop3邮件的代码
    码农架构技术周刊 | 第1期
    这样学Redis,才能技高一筹!
    CommunityServer2.1删除anonymous帐号后的解决办法
  • 原文地址:https://www.cnblogs.com/mtime2004/p/9775616.html
Copyright © 2011-2022 走看看