javascript - if else语句javascript使div根据.scrollTop改变颜色
问题描述
我是一名初学者编码器,我正在尝试根据网页向下滚动的距离来更改 div 的背景颜色,我哪里出错了?我需要放入一些东西来调用scrollTop金额吗?
(function () {
var scroll = .scrollTop;
if (scroll > 50) {
document.getElementByClassName("shop").style.backgroundColor = '#99C262';
}
else
{
document.getElementByClassName("shop").style.backgroundColor = ‘red’;
}
})();
解决方案
丢掉点,而不是“.shop”去“shop”</p>
而实际的函数 getElement s ByClassName 它返回一个带有类名的 div 集合。但是您需要第一个(假设您只有一个这样的 div),因此数组中的索引为 0
parentDOM.getElementsByClassName('test')[0].style.backgroundColor = "red";
<p class="test">hello here</p>
推荐阅读
- java - 为什么我在尝试发送 http 请求时收到 SSLProtocolExcepetion: Handshake failed ?
- javascript - 如何按如下定义的顺序创建数组?
- ios - Firebase 数据库 swift 的权限被拒绝
- windows - 您可以从主机 vm 连接到 vm 上的 dnn 网站吗
- scala - 带有列表和映射“查找”的 Scala 隐式值行为
- excel - 将 3 个表从 access 复制到现有的 excel 模板
- php - Php:如何在不提交表单的情况下检查复选框是否被选中?
- json - 使用 axios.get 条件加载大型 json 文件
- django - WSGI 应用程序的 SSL 证书
- java - java.io.InvalidClassException:本地类不兼容 - 不止一名工作人员的 Apache Spark 提交问题