javascript - 在Javascript中有条件地调用函数?
问题描述
我有一个称为截断文本的函数。仅当带有演示类的左 div 的 offsetheight 超过右 div 的 offsetheight 时,我如何有条件地调用该函数。如果不满足条件,则返回原始内容。
这是一个演示
var textHolder = document.querySelector('.demo');
var textHolder2 = document.querySelector('.demo2')
var textHolderHeight = textHolder.offsetHeight + 'px'
var textHolderHeight2 = textHolder2.offsetHeight + 'px'
console.log(textHolderHeight)
console.log(textHolderHeight2)
var fullText = textHolder.innerHTML;
var btn = document.querySelector('.btn');
var textStatus = 'full'; // use this to check the status of text and toggle;
function Truncate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
textStatus = 'truncated';
}
}
Truncate(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 {
Truncate(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>
<br><br><br>
<section class="demo2" id="demo2">
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!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!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>
提前谢谢你们。
解决方案
您可以在每次需要调用时使用 if 语句Truncate
。
if(textHolderHeight > textHolderHeight2){
Truncate(textHolder, textHolder.offsetWidth / 10);
}
或者将代码放在 Truncate 中,这样您就不必重复 if 语句。
function Truncate(textHolder, limit) {
let txt = textHolder.innerHTML;
if (txt.length > limit && textHolderHeight > textHolderHeight2) {
let newText = txt.substr(0, limit) + ' ...';
textHolder.innerHTML = newText;
textStatus = 'truncated';
}
}
推荐阅读
- reporting-services - 报表生成器 - 将查询的 where 子句中的一个数据集用于另一个数据集
- angular - 带角度的链式数组
- angular - Angular Universal html 语言标签
- python - Keras模型重复输出0,没有错误
- android - 在基本模块中使用导航组件的 Gradle 同步错误
- node.js - Socket.IO 套接字保护无需认证
- pyspark - 当列确实存在时,Pyspark 无法解析列名
- c# - C# 错误:委托是本地函数,因此必须始终有一个主体
- javascript - 按数据属性和父子关系对 li 排序
- mysql - 如何从 sqlx 获取最后插入的行的 id?