zoukankan      html  css  js  c++  java
  • jQuery中hover方法和toggle方法使用指南

    jQuery提供一些方法(如:toggle)将两种事件效果合并到一起,比如:mouseover、mouseout;keyup、keydown等
    1、hover函数
    hover(over,out)一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法。这是一个自定义的方法,它为频繁使用的任务提供了一种“保持在其中”的状态。
    参数:
    over (Function) : 鼠标移到元素上要触发的函数。
    out (Function): 鼠标移出元素要触发的函数。

    <script type="text/javascript">
    $(function(){
        $("#panel h5.head").hover(function(){
            $(this).next().show();// 鼠标悬浮时触发
        },function(){
            $(this).next().hide();// 鼠标离开时触发
        })
    })
    </script>
    
    

    2、toggle函数
    toggle(fn,fn) 每次点击时切换要调用的函数。如果点击了一个匹配的元素,则触发指定的第一个函数,当再次点击同一元素时,则触发指定的第二个函数。随后的每次点击都重复对这两个函数的轮番调用。 可以使用unbind("click")来删除。

    <script type="text/javascript">
    $(function(){
        $("#panel h5.head").toggle(function(){
            $(this).next().show();// 第一次点击时触发
        },function(){
            $(this).next().hide();// 第二次点击时触发,之后不停的切换
        })
    })
    </script>
    

    toggle() 方法切换元素的可见状态。
    如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素。toggle()方法切换元素的可见状态。
    如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素。
    所以上述的代码还可以写成:

    <script type="text/javascript">
    $(function(){
        $("#panel h5.head").toggle(function(){
             $(this).next().toggle();
        },function(){
             $(this).next().toggle();
        })
    })
    /*$(function(){
        $("#panel h5.head").click(function(){
             $(this).next().toggle();
        })
    })*/
    </script>
    

    还可以添加一些css样式:

    
    <style type="text/css">
    .highlight{ background:#FF3300; }
    </style>
    <script type="text/javascript">
    $(function(){
        $("#panel h5.head").toggle(function(){//配合css样式使用
            $(this).addClass("highlight");
            $(this).next().show();
        },function(){
            $(this).removeClass("highlight");
            $(this).next().hide();
        });
    })
    </script>
    
  • 相关阅读:
    EMC、Pure和NetApp推新品,NAS闪存场景在哪里
    Tomcat 开启Gzip压缩
    win10+ubuntu双系统安装方案
    游戏中水的渲染技术系列一
    什么时候用到线程
    高并发和多线程
    angularJS双向绑定和依赖反转
    javascript ES6
    angularJS核心原理
    javascript限定输入textarea输入长度
  • 原文地址:https://www.cnblogs.com/caobiin/p/7654970.html
Copyright © 2011-2022 走看看