jQuery - 切换文本以在单击时更改

时间:2014-03-08 00:26:34

标签: javascript jquery toggle

我有一个菜单按钮,点击后会向下滑动一个div。

我的菜单是标题“+打开菜单”

单击菜单按钮后,我的代码会将标题更改为“+菜单”,表示菜单已打开。

当用户关闭菜单时,我希望标题恢复为“ - 菜单”

这是我定义类的HTML,在这种情况下测试。

<span class="test">+ Open menu</span>

这是我的jQuery函数,我无法弄清楚如何恢复菜单状态。

$(document).ready(function () {
$(".widget_head_type").click(function () 
{
    $(".widget_body_type").slideToggle("slow");
    $(".test").text("- Close menu");
});

});

5 个答案:

答案 0 :(得分:11)

$(".widget_head_type").click(function () {
    $(".widget_body_type").slideToggle("slow");
    ($(".test").text() === "+ Open menu") ? $(".test").text("- Close menu") : $(".test").text("+ Open menu");
});

答案 1 :(得分:1)

jQuery.fn.extend({
    toggleText: function (a, b){
        var that = this;
            if (that.text() != a && that.text() != b){
                that.text(a);
            }
            else
            if (that.text() == a){
                that.text(b);
            }
            else
            if (that.text() == b){
                that.text(a);
            }
        return this;
    }
});

用作:

$("#YourElementId").toggleText('After', 'Before');

答案 2 :(得分:1)

以下是基于http://api.jquery.com/toggle/

的有趣内容

HTML

<span class="test">+ Open menu</span>
<span class="test" style="display:none">- Close menu</span>

JS

$(document).ready(function () {
$(".widget_head_type").click(function () 
{
    $(".widget_body_type").slideToggle("slow");
    $(".test").toggle();
});

简单,只需使用类测试切换所有标记,无论是隐藏还是可见。

答案 3 :(得分:0)

WORKING FIDDLE HERE

我已经猜到了你的HTML,并为替代品做了一些注释,但你考虑以下几点:

HTML:

<div class="some_parent_class">
    <div class="widget_head_type">
        <span class="test">+ Menu</span>
    </div>
    <div class="widget_body_type">
        Body
    </div>
</div>

JS:

jQuery(function ($) {
    $('.widget_head_type').each(function () {
        // closures
        var $toggle = $(this);
        var $parent = $toggle.closest('.some_parent_class');
        var $target = $parent.find('.widget_body_type');
        var $label = $toggle.find('.test');
        var bIsTweening = false;
        // OR var $target = $toggle.next('.widget_body_type');
        // event methods (closures)
        var fClickToggle = function () {
            if (!bIsTweening) {
                bIsTweening = true;
                $target.slideToggle('slow', fToggleCallback);
            }
        };
        var fToggleCallback = function () {
            bIsTweening = false;
            fSetLabel();
        };
        var fSetLabel = function () {
            var sLabel = $label.text().replace('+', '').replace('-', '');
            sLabel = ($target.is(':visible') ? '-' : '+') + sLabel;
            $label.html(sLabel);
        };
        // handle event
        $toggle.click(fClickToggle);
        $target.hide();
        fSetLabel();
    });
});

答案 4 :(得分:0)

有同样的问题,这就是我解决它的方法:使用你的代码示例。

$(".widget_head_type").click(function () {
    $(".widget_body_type").slideToggle("slow");
    ($(".test").text() == "+ Open menu") ? $(".test").text("- Close menu") : $(".test").text("+ Open menu");
});

使用两个相等的符号进行比较。

相关问题