我想选择文档中的每个元素,并在滚动到它们时将它们设为红色。
$(document).ready(function() {
$(document).on("scroll", animationDivs);
function animationDivs(event) {
var scrollPos = $(document).scrollTop();
var divs = $("*");
$(divs).each(function() {
var currLink = $(this);
if (currLink.position().top <= scrollPos && currLink.position().top + currLink.height() > scrollPos) {
currLink.style.color = "red";
}
});
};
});
我使用了这个代码但没有用。
最佳答案
使用 JS:
document.querySelectorAll('*')
.forEach(el => el.style.color = 'red')
在浏览器的控制台中尝试它,看看它是如何工作的 here's使用 JS 与 jQuery 选择 DOM 的简要概述。
This是一个类似的问题,有多种解决方案。
关于javascript - 如何使用 JS 或 jQuery 选择每个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54729328/