简体   繁体   English

切换div后更改文本

[英]Change text upon toggle div

How can I get the toggle to say Less Information when clicked? 单击时如何显示“较少信息”? I tried using the::after prompt in css but couldn't get it working. 我尝试在CSS中使用:: after提示,但无法正常工作。 I also want to style the 'Less Information' in css. 我也想在CSS中设置“较少信息”的样式。 Maybe I should make it call upon a class? 也许我应该让它上课吗?

<div class="toggle">More Information</div>

$(document).ready(function(){
  $(".content").hide();

  $(".toggle").on("click", function(e){   
      $(this).next('.content').slideToggle(200); 

    });
});

Use .text() to replace the text inside the div . 使用.text()替换div的文本。

Try this: 尝试这个:

$(document).ready(function(){
  $(".content").hide();
  $(".toggle").on("click", function(){   
      $(this).next('.content').slideToggle(200);
      $(this).text($(this).text() == 'More Information' ? 'Less Information' : 'More Information');
    });
});

JSFiddle Demo JSFiddle演示

UPDATE UPDATE

You could also use :visible to check if content is visible and change text accordingly. 您也可以使用:visible检查内容是否可见,并相应地更改文本。

$(document).ready(function () {
    $(".content").hide();
    $(".toggle").on("click", function () {
        var txt = $(".content").is(':visible') ? 'More Information' : 'Less Information';
        $(".toggle").text(txt);
        $(this).next('.content').slideToggle(200);
    });
});

JSFiddle Demo JSFiddle演示

Could do like this: Demo http://jsfiddle.net/f1dmhf0d/ 可能会这样: 演示 http://jsfiddle.net/f1dmhf0d/

Detail safely assuming your content is above "more info" you can traverse through the dom and use :visible flag aa=ccordingly to change the text using .text 如果您的内容位于“更多信息”之上,则可以安全地进行详细说明,您可以遍历dom并使用:visible标志aa = ccording来使用.text更改文本

Hope rest fits your need. 希望休息能满足您的需求。 :)

code

$(document).ready(function () {
    $(".content").hide();

    $(".toggle").on("click", function (e) {

        var $this = $(this).prev('.content');
        var $text = $(this);
        $this.slideToggle('slow', function () {
            if ($(this).is(':visible')) {
                $text.text('less info');
            } else {
                $text.text('more info');
            }
        });

    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM