jQuery:点击更改按钮文字

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

这是我所做的一个小提琴: 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'); } }); 

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

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

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

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

试试这个,这个javascript代码一直改变文本点击按钮。 http://jsfiddle.net/V4u5X/2/

HTML代码

  

JavaScript的

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

这应该适合你:

  $('.SeeMore2').click(function(){ var $this = $(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'); } });

在HTML中:

  

在Jquery中写这个函数:

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