jQuery的切换按钮上的文字 [英] Toggling button text in jquery

查看:1282
本文介绍了jQuery的切换按钮上的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个HTML按钮,我想切换正在使用的JavaScript / JQuery的两个文本之间显示在按钮上的文本。我该怎么做呢?

I have an HTML button created and I want to toggle the text that is displayed in the button between two text using Javascript/JQuery. How do I do this?

目前,我有:

<button onclick="showAll();" class="collapse-classes">
    <span class="button-text">Show</span>
</button>

按钮一次及以后点击后通过显示显示开头,然后按下开关时,隐藏,然后切换到显示。我试图改变变量的值,但它不会改变显示的文本。任何人都可以用脚本帮助吗?谢谢

The button starts off by displaying "Show" and then switch to "Hide" when clicked and then switch to "Show" when clicked again and onward. I tried changing the value of the tag but it doesn't change the text displayed. Can anyone help with the script? thanks

推荐答案

不要使用的onclick 。只需绑定一个事件处理程序。

Don't use onclick. Just bind an event handler.

下面是一些你可以使用的工作:

Here's something you can work with:

$('.collapse-classes').click(function() {
    var $this = $(this);

    $this.toggleClass('show');

    if ($this.hasClass('show')) {
        $this.text('Show');
    } else {
        $this.text('Hide');
    }
});

这篇关于jQuery的切换按钮上的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆