javascript - 为什么具有相同属性的两个对象不能以相同的方式工作
问题描述
在 Javascript30 中执行任务时,我注意到
document.querySelectorAll('input')[0].style.setProperty
并document.documentElement.style.setProperty
输出相同的对象,但当我尝试设置属性时前者不起作用。
我想知道为什么前者不起作用,但后者起作用。
我做了一个 console.log 来查看两行代码的输出。
let controller = document.querySelectorAll(".controller input");
//console.log(document.querySelectorAll('input')[0].style.setProperty);
//console.log(document.documentElement.style.setProperty);
function handleChange() {
const suffix = this.dataset.sizing || "";
document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
/*document.querySelectorAll('input').forEach((input) => {
input.style.setProperty(`--${this.name}`, this.value + suffix);
});*/
}
controller.forEach(input => input.addEventListener('change', handleChange));
controller.forEach(input => input.addEventListener('mousemove', handleChange));
body {
text-align: center;
color: white;
background-color: rgb(150, 200, 140);
}
:root {
--blur: 10px;
--spacing: 10px;
--color: red;
}
img {
padding: var(--spacing);
filter: blur(var(--blur));
background: var(--color);
}
<header>Playing with CSS variables and JS</header>
<div class="controller">
<label for="spacing">Spacing: </label>
<input type="range" min="10" max="200" id="spacing" name="spacing" value="10" data-sizing="px">
<label for="blur">Blur: </label>
<input type="range" min="0" max="30" id="blur" name="blur" value="10" data-sizing="px">
<label for="color">Base Color</label>
<input type="color" id="color" name="color">
</div>
<img src="https://res.cloudinary.com/dzwmmrwr2/image/upload/v1542708495/6_kmfxtt.png" alt="image" width="300" height="350">
解决方案
问题是您正在使用 querySelector 选择“输入元素”,而不是像使用document.documentElement
使用document.querySelectorAll('html')
而不是document.querySelectorAll('input')
解决您的问题:
// Code goes here
let controller = document.querySelectorAll(".controller input");
function handleChange() {
const suffix = this.dataset.sizing || "";
document.querySelectorAll('html').forEach((input) => {
input.style.setProperty(`--${this.name}`, this.value + suffix);
});
}
controller.forEach(input => input.addEventListener('change', handleChange));
controller.forEach(input => input.addEventListener('mousemove', handleChange));
body {
text-align: center;
color: white;
background-color: rgb(150, 200, 140);
}
:root {
--blur: 10px;
--spacing: 10px;
--color: red;
}
img {
padding: var(--spacing);
filter: blur(var(--blur));
background: var(--color);
}
<header>Playing with CSS variables and JS</header>
<div class="controller">
<label for="spacing">Spacing: </label>
<input type="range" min="10" max="200" id="spacing" name="spacing" value="10" data-sizing="px">
<label for="blur">Blur: </label>
<input type="range" min="0" max="30" id="blur" name="blur" value="10" data-sizing="px">
<label for="color">Base Color</label>
<input type="color" id="color" name="color">
</div>
<img src="https://res.cloudinary.com/dzwmmrwr2/image/upload/v1542708495/6_kmfxtt.png" alt="image" width="300" height="350">
推荐阅读
- mockito - Mokito - java.lang.NoClassDefFoundError: org/powermock/core/classloader/MockClassLoader
- javascript - 无法将 id 或元素传递给 mongoose 参数
- nestjs - 在异常过滤器中读取异常情况下的响应
- javascript - React Table isLoading 属性
- python - 稍后修改函数
- sql-server - 列的秒表
- java - 查询包含 ArrayList 类型属性的实体
使用 spring data jpa 包含一组特定的字符串 - swift - 从哈希值计算中省略一个属性
- ios - 内存增加但在关闭时不释放 [Mapbox]
- python - 在带有类方法的 Pandas 中使用 pipe()?