JQuery: изменить текст кнопки при нажатии
После просмотра Переключить текст кнопки в jquery этот вопрос, я попытался создать его в своей ситуации, но не могу показаться, что он работает.
Вот скрипка того, что я сделал: 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. Что мне не хватает?
Ответы
Ответ 1
$('.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/
Ответ 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
Это должно сработать для вас:
$('.SeeMore2').click(function(){
var $this = $(this);
$this.toggleClass('SeeMore2');
if($this.hasClass('SeeMore2')){
$this.text('See More');
} else {
$this.text('See Less');
}
});
Ответ 4
его короткий рабочий код
$('.SeeMore2').click(function(){
var $this = $(this).toggleClass('SeeMore2');
if($(this).hasClass('SeeMore2'))
{
$(this).text('See More');
} else {
$(this).text('See Less');
}
});