jQuery:点击更改按钮文字

时间:2022-11-20 14:12:15

After seeing Toggling button text in jquery this question, I tried to re create it in my situation but can't seem to have it work.

在jquery看到Toggling按钮文本这个问题之后,我试着在我的情况下重新创建它,但似乎无法让它工作。

Here is a fiddle of what I've done: http://jsfiddle.net/V4u5X/

这是我所做的一个小提琴:http://jsfiddle.net/V4u5X/

$('.SeeMore2').click(function(){
    var $this = $(this);
    $this.toggleClass('SeeMore');
    if($this.hasClass('.SeeMore2')){
        $this.text('See More');         
    } else {
        $this.text('See Less');
    }
});

It seems to only ever run the if statement once. What am I missing?

它似乎只运行一次if语句。我错过了什么?

5 个解决方案

#1


34  

$('.SeeMore2').click(function(){
    var $this = $(this);
    $this.toggleClass('SeeMore2');
    if($this.hasClass('SeeMore2')){
        $this.text('See More');         
    } else {
        $this.text('See Less');
    }
});

This should do it. You have to make sure you toggle the correct class and take out the "." from the hasClass

这应该做到这一点。你必须确保切换正确的类并取出“。”。来自hasClass

http://jsfiddle.net/V4u5X/2/

#2


4  

try this, this javascript code to change text all time to click button.http://jsfiddle.net/V4u5X/2/

试试这个,这个javascript代码改变文本所有时间点击按钮.http://jsfiddle.net/V4u5X/2/

html code

<button class="SeeMore2">See More</button>

javascript

$('.SeeMore2').click(function(){
        var $this = $(this);
        $this.toggleClass('SeeMore2');
        if($this.hasClass('SeeMore2')){
            $this.text('See More');         
        } else {
            $this.text('See Less');
        }
    });

#3


1  

This should work for you:

这应该适合你:

    $('.SeeMore2').click(function(){
        var $this = $(this);
        $this.toggleClass('SeeMore2');
        if($this.hasClass('SeeMore2')){
            $this.text('See More');         
        } else {
            $this.text('See Less');
        }
});

#4


1  

its work short code

它的工作短代码

$('.SeeMore2').click(function(){ var $this = $(this).toggleClass('SeeMore2'); if($(this).hasClass('SeeMore2')) { $(this).text('See More');
} else { $(this).text('See Less'); } });

$('。SeeMore2')。click(function(){var $ this = $(this).toggleClass('SeeMore2'); if($(this).hasClass('SeeMore2')){$(this)。 text('See More');} else {$(this).text('See Less');}});

#5


0  

In HTML:

<button type="button" id="AddButton" onclick="AddButtonClick()" class="btn btn-success btn-block ">Add</button> 

In Jquery write this function:

在Jquery中写这个函数:

    function AddButtonClick(){ 
      //change text from add to Update
      $("#AddButton").text('Update');
    }

#1


34  

$('.SeeMore2').click(function(){
    var $this = $(this);
    $this.toggleClass('SeeMore2');
    if($this.hasClass('SeeMore2')){
        $this.text('See More');         
    } else {
        $this.text('See Less');
    }
});

This should do it. You have to make sure you toggle the correct class and take out the "." from the hasClass

这应该做到这一点。你必须确保切换正确的类并取出“。”。来自hasClass

http://jsfiddle.net/V4u5X/2/

#2


4  

try this, this javascript code to change text all time to click button.http://jsfiddle.net/V4u5X/2/

试试这个,这个javascript代码改变文本所有时间点击按钮.http://jsfiddle.net/V4u5X/2/

html code

<button class="SeeMore2">See More</button>

javascript

$('.SeeMore2').click(function(){
        var $this = $(this);
        $this.toggleClass('SeeMore2');
        if($this.hasClass('SeeMore2')){
            $this.text('See More');         
        } else {
            $this.text('See Less');
        }
    });

#3


1  

This should work for you:

这应该适合你:

    $('.SeeMore2').click(function(){
        var $this = $(this);
        $this.toggleClass('SeeMore2');
        if($this.hasClass('SeeMore2')){
            $this.text('See More');         
        } else {
            $this.text('See Less');
        }
});

#4


1  

its work short code

它的工作短代码

$('.SeeMore2').click(function(){ var $this = $(this).toggleClass('SeeMore2'); if($(this).hasClass('SeeMore2')) { $(this).text('See More');
} else { $(this).text('See Less'); } });

$('。SeeMore2')。click(function(){var $ this = $(this).toggleClass('SeeMore2'); if($(this).hasClass('SeeMore2')){$(this)。 text('See More');} else {$(this).text('See Less');}});

#5


0  

In HTML:

<button type="button" id="AddButton" onclick="AddButtonClick()" class="btn btn-success btn-block ">Add</button> 

In Jquery write this function:

在Jquery中写这个函数:

    function AddButtonClick(){ 
      //change text from add to Update
      $("#AddButton").text('Update');
    }