如何将文本从“截断”切换为“全文”?
就像我想在一个人单击“阅读更多”按钮时切换全文,并在单击“隐藏文本”按钮时也隐藏文本
片段:
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)