小编The*_*heV的帖子

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

这里的例子: 在此处输入图片说明

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

就像我想在一个人单击“阅读更多”按钮时切换全文,并在单击“隐藏文本”按钮时也隐藏文本

片段:

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);
Run Code Online (Sandbox Code Playgroud)
<section class="demo" id="demo">
  Let's truncate a long line of text so that the …
Run Code Online (Sandbox Code Playgroud)

javascript html5 css3

3
推荐指数
1
解决办法
907
查看次数

标签 统计

css3 ×1

html5 ×1

javascript ×1