如何使用JQuery在“更多”按钮中添加动态内容

时间:2018-07-23 17:04:42

标签: jquery

我正在尝试在“ jQuery更多信息”按钮中添加动态内容。当我们在show-read-more类中添加任何HTML标记时,它不起作用。帮我解决这个问题。

$(document).ready(function() {
  var maxLength = 100;
  $(".show-read-more").each(function() {
    var myStr = $(this).text();
    if ($.trim(myStr).length > maxLength) {
      var newStr = myStr.substring(0, maxLength);
      var removedStr = myStr.substring(maxLength, $.trim(myStr).length);
      $(this).empty().html(newStr);
      $(this).append(' <a href="javascript:void(0);" class="read-more">(read more)</a>');
      $(this).append('<span class="more-text">' + removedStr + '</span>');
    }
  });
  $(document).on("click", ".read-more", function() {
    $(this).siblings(".more-text").contents().unwrap();
    $(this).remove();
  });
});
.show-read-more .more-text {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="show-read-more">
  <h1>Lorem Ipsum</h1> is simply
  <p>dummy text of the printing and typesetting industry. Lorem Ipsum has been the industries standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only
    five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software
    like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>

0 个答案:

没有答案