在.click()函数期间替换按钮文本

时间:2011-04-15 15:30:05

标签: jquery

我有一个隐藏的文本框。如果单击按钮,则会显示div的内容。如果我再次单击Buutton,内容将消失(.slideToggle()):如果div的内容可见并将其更改回来,如果再次单击该按钮并且内容,我想替换我的按钮文本DIV,更好:DIV被隐藏了。 那是我目前的Coce:

function show_content(){
    jQuery('.single_content').hide();
    jQuery('.css_hide').show();
    jQuery('.show_content').click(function(){
        jQuery('.single_content').slideToggle();
        var text = jQuery(this).html();
        if ( jQuery('.single_content').is(':visible') ) {
            jQuery(this).html('abc');   
        }
    });
}

我做错了什么?

非常感谢

3 个答案:

答案 0 :(得分:2)

由于按钮是input元素,因此您需要设置value来更新文字:

jQuery(this).val('abc');

答案 1 :(得分:2)

尝试类似:

function show_content(){
jQuery('.single_content').hide();
jQuery('.css_hide').show();
jQuery('.show_content').click(function(){
    jQuery('.single_content').slideToggle();
    var text = jQuery(this).html();
    if ( jQuery('.single_content').is(':visible') ) {
    jQuery(this).attr('value','ABC');   
    }
});

答案 2 :(得分:0)

您的代码应该类似于:

function show_content(){
    $('.single_content').hide();
    $('.css_hide').show();
    var text = "";
    $('.show_content').click(function(){
        var $this = $(this);
        var $single_content = $('.single_content');

        $single_content.slideToggle();

        if ( $single_content.is(':visible') ) {
            $this.val('abc');   
            text = j$this.val();
        }
        else{
            $this.val(test);   
        }
    });
}

这里我使用$来简洁并缓存了一些对象