jquery - 即使切换课程,背景图像也会保留
问题描述
我不确定如何正确解释这一点,但这应该作为一个常规的阅读更多按钮来切换文本和图像(图标)。但是,由于某种原因,切换类不会删除其中一个图像,而它适用于另一个图像,因此两个图像中的一个始终可见。
这是html
<div class="read-more more">
<button class="read-more_button">Read more</button>
</div>
这是我用来切换 read more div 的类的 jQuery。它总是从“更多”类开始。
$(".read-more").click(function(){
$(this).toggleClass("more less");
$(".content-sidebar").toggleClass("longtext");
if( $(this).hasClass("less") ) {
$(".read-more_button").html("Hide");
} else if( $(this).hasClass("more") ) {
$(".read-more_button").html("Read more");
}
});
编辑:这是用于添加图标的 css (sass)
.read-more {
position: absolute;
bottom: 0;
left: 0;
height: 100px;
right: 0;
border-radius: 0 0 5px 5px;
margin: 0 15px;
cursor: pointer;
background: none;
&.more {
background: (gradient but removed for readability)
button.read-more_button {
background: url('assets/plus-more.png') 0 / 14px no-repeat transparent;
}
}
&.less {
background: transparent;
button.read-more_button {
background: url('assets/minus-less.png') 0 / 14px no-repeat transparent;
}
}
button.read-more_button {
position: absolute;
bottom: 15px;
left: 30px;
right: 30px;
border: none;
background: none;
padding: 0 0 0 20px;
color: $gray-dark;
cursor: pointer;
outline: none;
}
}
这两个类都有一个图标集作为背景图像。“更多”有一个加号图标,“更少”有一个减号图标。“更少”的图标可以正常工作。对于“更多”,情况并非如此。即使将“更多”类切换为“更少”,加号图标仍然可见。我怎样才能使加号和减号图标仅在他们的课程处于活动状态时显示?
解决方案
推荐阅读
- android - 调用函数更新应用时,Ionic Force App Update 崩溃
- linux - getopts在选项之前有参数时行不通
- java - 将 Java 十六进制转换为十进制时出现错误
- algorithm - O(1) 时间复杂度中以下 ncr 系列的总和
- html - 移动设备上的引导堆栈元素顶部和底部,桌面上的侧边栏
- android - Firebase Android 重置后发送新密码
- java - H2 在内存中创建服务器以通过 Shell 访问
- vagrant - Vagrant 同步文件夹导致内容被删除
- c++ - 如何将 std::stringstream 转换为 uintptr_t 并保持十六进制值?
- html - 谷歌浏览器在使用时抱怨闪退