jquery - 阅读更多/更少使用 jquery 而不丢失 html

标签 jquery

我编写了 jquery 在 250 个字符后添加多读/少读。我已经实现了以下查询:

var minimized_elements = $('.field-type-text-with-summary .field-items');
var minimize_character_count = 250;    
minimized_elements.each(function(){    
  var TextContent = $(this).text();
  var TextContentLenght = TextContent.length;
  var t = $(this).html();
  if(t.length < minimize_character_count ) return;
  $(this).html(
    t.slice(0,minimize_character_count)+
      '<span>...</span>'+'<a href="#" class="read_more"     style="color:#FF8403;">Read more</a>'+
      '<span style="display:none;">'+ TextContent.slice(minimize_character_count,TextContentLenght)+'&nbsp'+'<a href="#" class="read_less" style="color:#FF8403;">Read less</a></span>'
    );  
  });
  $('a.read_more', minimized_elements).click(function(event){
    event.preventDefault();
    $(this).hide().prev().hide();
    $(this).next().show(); 

  });
  $('a.read_less', minimized_elements).click(function(event){
    event.preventDefault();
    $(this).parent().hide().prev().show().prev().show();

  });

但我的问题是这个脚本删除了所有 html 标签,例如如果文本包含粗体或下划线字符,则会将所有文本显示为纯文本。我想通过所有格式阅读更多/更少。请建议。提前非常感谢。

最佳答案

    <!doctype html>

<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
.readLess{ display:none;}
.conteMain{ border:1px solid #ddd; background:#f5f5f5; padding:10px; font:normal 14px Arial, Helvetica, sans-serif;}
#conteMain p{ margin:0; padding:0;}
.readLess,.readMore{ background:#3CF; color:#fff; padding:5px 10px; width:80px; margin:10px 0px; cursor:pointer}
.readLess:hover,.readMore:hover{ background:#090}
</style>
</head>

<body>
<div class="conteMain">
<div id="conteMain">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's 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.<br>
<br>
<p>vived 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 </p><br>

<p>vived 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 </p><br>

<p>vived 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 </p><br>

<p>vived 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 </p><br>

<div>ved 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 l</div><br>


 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.
</div>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
    var conte = $("#conteMain").text().length;
    var conte1 = $("#conteMain").text();
    var bx1 = conte1.slice('',200); 

    if(conte > 200){
        $("#conteMain").after("<span class='moreC'>"+ bx1 +"</span>"+"<div class='readMore'>Read More</div><div class='readLess'>Read Less</div>");
        $("#conteMain").css("display","none");
    };

    $(".readMore").click(function(){
        $("#conteMain").slideDown("slow");
        $(".moreC").css("display","none");
        $(".readLess").css("display","block");
        $(this).css("display","none");
    });

    $(".readLess").click(function(){
        $("#conteMain").slideUp("slow");
        $(".moreC").css("display","block");
        $(".readMore").css("display","block");
        $(this).css("display","none");
    });

</script>
</body>
</html>

关于jquery - 阅读更多/更少使用 jquery 而不丢失 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38726624/

相关文章:

jquery - 生成随机颜色 jQuery 不添加 css

jquery - 弹出js代码不起作用

javascript - 使用来自 w3data 的包含 html,JavaScript ( jquery ) 不工作

jquery - 创建自定义 Flexslider 导航箭头时出现问题

jquery - 扩展 jQuery 插件的最佳方式

jquery - HTML DOM 和 JQuery 哪个更高效

javascript - 在后台重新加载页面

jquery - 如何在使用 jquery 表单提交时从 golang 重定向到另一个模板?

javascript - 使用 jQuery 旋转背景图像

javascript - .toggle() 函数不起作用