`

Query之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>

 

文章来源:http://www.hxstrive.com/article/73.htm

 

 

 

分享到:
评论

相关推荐

    query-string:处理查询字符串

    请求参数 ...composer require spatie/query-string 用法 use Spatie \ QueryString \ QueryString ; $ queryString = new QueryString ( $ uri ); 切换参数 一次切换 # / &gt; /?toggle $ queryString -&gt;

    math-kernel-toggle:Alfred 3工作流程来切换Mathematica内核进行计算

    cd到您的workflows文件夹并执行: $ git clone git@github.com:LingyuanJi/math-kernel-toggle.git 警告 你需要有一个Mathematica 11安装到你的Mac系统,用命令行可执行文件一起/usr/local/bin/wolframscript

    bootstrap table操作技巧分享

    本文实例为大家分享了bootstrap table操作的相关技巧,供大家参考,具体内容如下 源码 ... &lt;table id="agenttable" data-toggle="table" ... data-query-params="bh_agt_queryParams" data-search="true" d

    jQuery详细教程

    • 把所有事件处理函数置于文档就绪事件处理器中 • 把 jQuery 代码置于单独的 .js 文件中 • 如果存在名称冲突,则重命名 jQuery 库 jQuery 事件 下面是 jQuery 中事件方法的一些例子: Event 函数 绑定函数至 $...

    Android代码-CallbackDialer

    Widget provides toggle button for enable/disable caller. Caller makes http query to web callback script on your home server. Callback script should accept from and to parameters and create asterisk ...

    remotedroid:与Ruby-MacroDroid相关的实验,用于触发宏和远程响应操作

    # toggle the torch r.invoke :torch # retrieve the current location of the device location = r.query :location #=&gt; {:coords=&gt;"55.9192166,-3.246983", :latency=&gt;1.496} # play a chirpy sound r = r.invoke...

    CnPlugin_1.5.4

    Query data using alias 以字段注释字符作为字段名拼出查询SQL语句(选中表名出现) Open in new SQL Window 复制当前窗口选中的SQL语句到新窗口中(选中字符串是出现) Execute in new SQL Window 复制当前窗口...

    Joomla! 1.5 Development Cookbook.pdf

    Creating toggle-enabled order column headers for tabular data in an MVC component 222 Ordering tabular data in an MVC component 224 This material is copyright and is licensed for the sole use by ...

    JS组件Bootstrap Table使用方法详解

    &lt;table data-toggle="table"&gt; &lt;thead&gt; ... &lt;/thead&gt; &lt;/table&gt; ... [xhtml] view plain copy $('#table').bootstrapTable({ url: 'data.json' }); 第二种方式交第一种而言在处理复杂数据时更为灵活,...

    plug-In PHP- 100 Power Solutions

    Query Highlight; Rolling Copyright; Embed YouTube Video; Create List; Hit Counter; Referer Log; Evaluate Expression; Validate Credit Card; Create Captcha; Check Captcha; Validate Text; Validate E-...

    FairyGUI-Unity-Plugin-3.4.0.zip

    - NEW: Add API to query package dependencies. - IMPROVED: Graphics module refactor. Now it is more convenient to create various shapes(pie, lines, polygon etc) and do mesh deform. Memory usage on ...

    Bochs - The cross platform IA-32 (x86) emulator

    - GUI: select mouse capture toggle method in .bochsrc. - Ported most of Qemu's 'virtual VFAT' block driver (except runtime write support, but plus FAT32 suppport) - Added write protect option for ...

Global site tag (gtag.js) - Google Analytics