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>
    
  • 相关阅读:
    还来一篇说下json_value 以及 json_query 的应用 (3)
    继续说一下openjson 以及 json path 的使用 (2)
    浅谈数据库资源使用的按需分配方法
    AlwaysON同步的原理及可用模式
    AlwaysON同步性能监控的三板斧
    为什么完整备份不能截断事务日志
    解读SQL 内存数据库的细节
    收缩SQL Server日志不是那么简单的(翻译)
    没有了SA密码,无法Windows集成身份登录,DBA怎么办?
    数据库错误日志惹的祸
  • 原文地址:https://www.cnblogs.com/caobiin/p/7654970.html
Copyright © 2011-2022 走看看