jQuery text()改变了toggle()? [英] jQuery text() change on toggle()?

查看:122
本文介绍了jQuery text()改变了toggle()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个更改切换链接文本的脚本,具体取决于其他元素的可见性。

I want to make a script that is changing toggle link text depending on others element visibility.

所以当#form可见时,我希望#form-container a文本是隐藏...,虽然它是隐藏的,但我希望文本是显示...。

So while #form is visible I want the #form-container a text to be "Hide...", and while it's hidden I want the text to be "Show...".

我试过这行 - 如果($('#form')。is(:visible)){另一种方式:if($('#form')。is(:visible)==true){ - 但它也没有不行。

I've tried this line - if($('#form').is(":visible")){ another way: if($('#form').is(":visible") == "true"){ - but it also doesn't work.

怎么了?每次切换另一个项目时如何更改文本?

What's wrong? How to change text every time another item is toggled?

$('.toggle').click(
    function()
    {
        $('#form').slideToggle();

            if($('#form').is(":visible")){
                $('#form-container a').text("Hide form container");
            }
            else {
                $('#form-container a').text("Show form container");  
            } 
    });

谢谢。

推荐答案

在动画制作时,它总是可见,您可以在 .slideToggle() 回调所以它检查完成动画的时间,如下所示:

It'll always be visible while animating, you can check the visibility in the .slideToggle() callback so it checks when it finishes animating, like this:

$('.toggle').click(function() {
  $('#form').slideToggle(function() {
    $('#form-container a').text(
      $(this).is(':visible') ? "Hide form container" : "Show form container"
    );
  });
});

这篇关于jQuery text()改变了toggle()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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