我有一个类名为“ concert-landing-synopsis”的部分,并且在滚动条上,我想检查该部分是否聚焦,并将类名添加到其他元素。我在这里通过一些解决方案进行了研究,重点变量始终为假
let activateElement = document.getElementsByClassName("concert-landing-synopsis");
if (activateElement.length > 0) {
window.onscroll = _ => {
let isFocused = (document.activeElement === activateElement[0]);
if (isFocused) {
console.log("On focus");
}
};
}
视图是这样的
<section class="concert-landing-details">
<div></div>
<div></div>
<div></div>
<div></div>
</section>
答案 0 :(得分:0)
您需要遍历document.getElementsByClassName('concert-landing-synopsis')
返回的所有元素,如
document.getElementsByClassName('concert-landing-synopsis').forEach((el) => {
const isFocused = (document.activeElement === el);
});