简体   繁体   English

jQuery Slide Toggle-打开时更改HTML

[英]jQuery Slide Toggle - change HTML when opened

I have this function to open up a DIV to show content when a "read more" link is clicked. 当单击“更多”链接时,我具有打开DIV来显示内容的功能。 I'd like to change the value of "Read More" to "Close" once the DIV has been expanded. 扩展DIV后,我想将“阅读更多”的值更改为“关闭”。

<div class="container">
  <p class="expand-text">Read more</p>
  <div class="content">
    <div class="full-text">
      <p>Text content here</p>
    </div>                      
  </div>
</div>                            

<script>
 $(".container").click(function() {
   $(this).find('.content').slideToggle();
 });
</script>
   $(".expand-text").click(function() {
       $('.container .content').slideToggle('slow', function() {
             if ( $('.container .content').css("display") == 'none') {
           $(".container .expand-text").html('Read more');
        } else {
            $(".container .expand-text").html('Close');
        }

       });

      });  
$(".container").click(function() {
    $('.expand-text', this).text(function(i, v) {
        return v === 'Close' ? 'Read more' : 'Close'
    })
    $(this).find('.content').slideToggle();
});

http://jsfiddle.net/mBFzE/ http://jsfiddle.net/mBFzE/

Try this 尝试这个

$(".container").click(function () 
{
   $(this).find('.content').slideToggle('slow',
           function () { $(".expand-text").text("Close"); });
});

Just provide a callback function which would be called when your animation will be completed. 只需提供一个回调函数,动画完成后即可调用该函数。 For more on this go here 有关更多信息, 请点击此处

You can pass a callback function to slideToggle() . 您可以将回调函数传递给slideToggle() It will be called when the animation ends, and from there you can update your element depending on the visibility of the content: 动画结束时将调用它,然后您可以根据内容的可见性从那里更新元素:

$(".container").click(function() {
    $(this).find(".content").slideToggle(400, function() {
        var $this = $(this);
        $this.closest(".container").find(".expand-text").text(
            $this.is(":visible") ? "Close" : "Read more");
    });
});

Here's a JS excerpt that could do what you're trying to do: 这是一个JS摘录,可以完成您想做的事情:

 $(".container").click(function() {
     var self = this;
     $(this).find('.content').slideToggle(function(){
         var newParagraphValue = undefined;
         var newParagraphValue = $(self).find('.full-text:visible').length > 0 ? "Close" : "Read more";                          
          $(self).find('.expand-text').html(newParagraphValue );    
          self = null;             
     });
 });​

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

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