jQuery切换和IF可见 [英] jQuery toggle and IF visible

查看:50
本文介绍了jQuery切换和IF可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在帐户管理页面上有一个div,其中包含设置和选项.

I have a div which contains settings and options on an account management page.

$("#moreOptions").slideToggle('slow');
if ($("#moreOptions").is(":visible") == true) {
    $("#lnkMoreOpt").text("Less Options «")
}
else {
    $("#lnkMoreOpt").text("More Options »")
}

上面的代码应根据是否可见来更改more/less options链接的文本,但是jQuery并未将切换视为使其不可见/可见.

The code above should change the text of the more/less options link depending on whether it is visible or not, however it appears jQuery does not treat toggling as making it invisible/visible.

如何在仍使用切换功能的情况下实现此功能?

How can I implement this while still using the toggle function?

推荐答案

您需要使用回调函数.到评估if语句时,slideToggle尚未完成,您将得到错误的结果.

You need to use the callback function. By the time the if statement is evaluated the slideToggle will not have completed and you will get incorrect results.

$("#moreOptions").slideToggle('slow', callbackFn);

function callbackFn(){

     var $link = $("#lnkMoreOpt");

     $(this).is(":visible") ? $link.text("Less Options «") : $link.text("More Options »");


}

这篇关于jQuery切换和IF可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆