【问题标题】:Jquery change text button show more / show lessJquery更改文本按钮显示更多/显示更少
【发布时间】:2014-03-21 22:41:29
【问题描述】:

我正在使用 jQuery 来隐藏或显示带有按钮的元素。 按钮值应更改为:Show more(隐藏元素时)和 show less(显示元素时)。

我的html代码

<section>
    <div class="contact_channel" name="<?php echo $this->id; ?>"></div>

    <div class="clearfix">
        <input type="button" class="showMore tiny button" value="Show more">
    </div>

    <div class="show_more" style="display:none;"></div>
</section>

我的 jQuery

<script type="text/javascript">

    $(document).ready(function () {
        $('.showMore').on('click', function () {
            var section = $(this).closest('section'),
                text    = section.find('.show_more'), // use classes, not ID
                state   = text.is(':hidden');
                button  = $(this).find('input');

            text[state?'slideDown':'slideUp'](500);
            button.prop('value', function() {
                return state ? "Show more" : "Show less";
            });
            $('input').show(200);
        });
    });
</script>

如您所见,这部分代码不起作用:

        button.prop('value', function() {
            return state ? "Show msdsdfs" : "Show lsdfsdfs";
        });
        $('input').show(200);

有什么想法吗?

【问题讨论】:

  • 如果我理解,你在 div 里面有“show_more”类的内容,当你点击按钮时它会显示内容(并改变按钮值)?
  • 是的,它会显示内容,但不会更改按钮值,这是我的问题

标签: javascript jquery html show-hide


【解决方案1】:

你不需要查找,因为按钮有 showMore 类

更改此代码

 button  = $(this).find('input');

 button  = $(this);

【讨论】:

  • 它工作正常,你能解释为什么它不工作吗?
  • .find 试图找到 .showMore 元素的子元素 - 您希望它作用于 .showMore 元素,而不是其子元素之一
猜你喜欢
  • 2020-02-15
  • 1970-01-01
  • 2020-06-18
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多