jQuery"if"改变文本

S. *_* E. 0 html jquery

这应该工作,还是我做错了什么?

HTML:

<div class="header-title">
<h1 class="h1-title" id='title'>America</h1>
</div>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

 $('#title').click(function(){
    var classExist = $(this).hasClass('active-t');
    if(classExist = false) {
        $('.header-title').html("<h1 class='h1-title'>'Murica!</h1>");
    }
    if(classExist = true) {
        $('.header-title').html("<h1 class='h1-title'>America</h1>");
    }
});
Run Code Online (Sandbox Code Playgroud)

的jsfiddle

无论如何,我不能让它工作

Ale*_*har 5

你可以简单:

 $('#title').click(function() {
   $(this).hasClass('active-t') ? $('.header-title').html("<h1 class='h1-title'>America</h1>") : $('.header-title').html("<h1 class='h1-title'>'Murica!</h1>");

 });
Run Code Online (Sandbox Code Playgroud)