如何在JavaScript中切换截断文本?

时间:2018-11-20 04:54:20

标签: javascript html5 css3

此处的示例: enter image description here

如何将文本从“截断”切换为“全文”?

就像我要在用户单击“阅读更多”按钮时切换全文,并在单击“隐藏文本”按钮时隐藏文本一样

摘要:

var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');


function Truancate(textHolder, limit) {
  let txt = textHolder.innerHTML;
  if (txt.length > limit) {
    let newText = txt.substr(0, limit) + ' ...';
    textHolder.innerHTML = newText;
  }
}

Truancate(textHolder, textHolder.offsetWidth / 10);



function toggleText() {
  // here i want to show full text...
  // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
}


btn.addEventListener('click', toggleText);
<section class="demo" id="demo">
  Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
  line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>

<button class="readMore btn">Read More</button>

3 个答案:

答案 0 :(得分:3)

您不需要javascript。一个简单的css text-overflow: ellipsis;就可以解决问题。

这是一种更好的/标准的截断长文本的方法,因为它将截断文本显示在准确的位置,这取决于字体大小,父容器的宽度等...这在js中是不可能的。这是一个演示:

var textHolder = document.querySelector('.demo');
var btn = document.querySelector('.btn');

function toggleText() {
  textHolder.classList.toggle("truncate");
}

btn.addEventListener('click', toggleText);
toggleText(); //to truncate at first time
.truncate {
  text-overflow: ellipsis;
  /*BOTH of the following are required for text-overflow (overflow: hidden; and white-space: nowrap;)*/
  overflow: hidden;
  white-space: nowrap;
}
<section class="demo" id="demo">
  Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
  Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
  Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>

<button class="readMore btn">Read More</button>

请注意,剪切innerHTML可能很危险,因为您可能在不适当的位置剪切并破坏了HTML代码的打开/关闭标签...

答案 1 :(得分:1)

首先,您需要将全文存储到一个变量中,永远不要更改该变量,然后将被截断的文本存储到另一个变量中,最后在这些变量和变量值之间切换以将文本设置为目标元素。

这是sinppet:

var textHolder = document.querySelector('.demo');
var fullText = textHolder.innerHTML;
var btn = document.querySelector('.btn');
var textStatus = 'full'; // use this to check the status of text and toggle;

function Truancate(textHolder, limit) {
  let txt = textHolder.innerHTML;
  if (txt.length > limit) {
    let newText = txt.substr(0, limit) + ' ...';
    textHolder.innerHTML = newText;
    textStatus = 'truncated';
  }
}

Truancate(textHolder, textHolder.offsetWidth / 10);

function toggleText() {
  // here i want to show full text...
  // and also -> btn.innerHTML = 'Hide Text' | 'Show Text;
  if (textStatus === 'truncated') {
    textHolder.innerHTML = fullText;
    textStatus = 'full';
  } else {
    Truancate(textHolder, textHolder.offsetWidth / 10);
  }
}


btn.addEventListener('click', toggleText);
<section class="demo" id="demo">
  Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
  line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>

<button class="readMore btn">Read More</button>

答案 2 :(得分:0)

这里是一些面向对象的方法,可以在类似情况下的任何地方使用

您只需要执行以下步骤

  1. 创建一个具有ID的包装容器,让我们说readMoreContainer

  2. 将名为readMoreText的类添加到包含要截断的文本的部分

  3. 将名为readMoreButton的类添加到按钮元素。

您可以删除不必要的类和ID

var ReadMore = (function() {
  function ReadMore(limit, readMoreContainerElementSelector) {
    this.limit = limit;
    this.readMoreContainerElementSelector = readMoreContainerElementSelector;
    this.isFullTextShown = false;
    this.initializeReadMore();
  }
  ReadMore.prototype.initializeReadMore = function() {
    this.fullText = document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML;
    this.truncatedText = this.fullText.substr(0, this.limit) + ' ...';
    var that = this;
    document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").addEventListener("click", function() {
      that.performToogle();
    });
    this.performToogle();
  };
  ReadMore.prototype.performToogle = function() {
    var textToDisplay = "";
    var buttonText = "";
    this.isFullTextShown ? (textToDisplay = this.fullText, buttonText = "Read Less" ): (textToDisplay = this.truncatedText, buttonText = "Read More");
    this.isFullTextShown = !this.isFullTextShown;
    document.querySelector(this.readMoreContainerElementSelector + " .readMoreText").innerHTML = textToDisplay;
    document.querySelector(this.readMoreContainerElementSelector + " .readMoreButton").innerHTML = buttonText;
  };
  return ReadMore;
}());

var readmore = new ReadMore(100, "#readMoreContainer1");
var readmore = new ReadMore(50, "#readMoreContainer2");
<div id="readMoreContainer1">
<section class="demo readMoreText" id="demo">
  Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
  line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>
<hr/>
<div id="readMoreContainer2">
<section class="demo readMoreText" id="demo">
  Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long line of text so that the words don't wrap when they're not supposed to. Multi-line of course! Let's truncate a long
  line of text so that the words don't wrap when they're not supposed to. Multi-line of course!
</section>
<button class="readMore btn readMoreButton">Read More</button>
</div>

相关问题