javascript - 按顺序排列的 JavaScript 事件
问题描述
我有一个按钮,这个按钮应该包装文本并解开它,我怎样才能让按钮在第一次单击时解开文本,然后在下一次单击时包装它?包装是由 property 完成的white-space
。
// split the state into two chunks
function fullView(e) {
// check if the element is the targeted or not
if (e.target.parentElement.className == 'note') {
// first state with the un-wrap text
if(e.target.style.whiteSpace == 'normal'){
// convert it to wraped
document.querySelector(".note-content").style.whiteSpace = "nowrap";
}else {
// convert it to un-warped text
document.querySelector(".note-content").style.whiteSpace = "normal";
}
}
}
<li class="note">
<p class="note-content">
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
</p>
</li>
解决方案
从 的内容来看,我假设您已将fullView
其设置为. 如果是这样,最简单的方法是定义一个 CSS 类:click
.note-content
.nowrap {
white-space: nowrap;
}
...在 中fullView
,切换它:
function fullView(e) {
e.target.classList.toggle("nowrap");
}
现场示例:
// split the state into two chunks
function fullView(e) {
e.target.classList.toggle("nowrap");
}
document.querySelector(".note-content").addEventListener("click", fullView);
.nowrap {
white-space: nowrap;
}
<li class="note">
<p class="note-content">
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
Hello, World Note !!
</p>
</li>
推荐阅读
- sql - 在触发器中使用更新
- dynamics-crm - 如何取消合并从 D365 OOB 功能合并的记录?
- codenameone - 代号一 构建时的图标和图片大小
- fish - 鱼壳中的 for 循环最多只能运行 3 次
- javascript - Nodejs:为什么 db.collection().find() 记录多次?
- activerecord - Yii2 只显示附加时间小于当前时间的数据
- html - Bootstrap 可见性实用程序在不应该隐藏元素时隐藏了它
- python - 如何在 SQLite3 for python 中合并两个查询的结果?
- html - 如何阻止 contenteditable div 溢出?
- javascript - 如何使离子元素漂浮在离子头上