javascript - 为什么 HTML 集合中的元素数量不等于 HTML 集合对象的长度?
问题描述
const profile = document.querySelector('#profile');
profile.addEventListener('mouseover', function() {
console.log('parent')
const onHover = document.createElement('div');
const hoverContent = document.createTextNode('my profile');
onHover.setAttribute('id', 'miniProfile');
onHover.append(hoverContent);
onHover.style.height = '100px';
onHover.style.width = '100px';
onHover.style.backgroundColor = 'blue';
onHover.style.position = 'absolute';
onHover.style.top = '30px';
this.append(onHover)
setTimeout(()=> {
onHover.style.top = '15px';
onHover.style.transition = '1s'
onHover.style.transitionTimingFunction = 'ease'
}, 10)
onHover.addEventListener('mouseover', function(e) {
// e.stopPropagation()
})
})
profile.addEventListener('mouseout', function(e) {
if(e.relatedTarget.id.toLowerCase() != this.children[0].id.toLowerCase()) {
console.log(this.children)
if(this.children.length != 0) {
for(let i = 0; i < this.children.length; i++) {
this.children[i].remove();
}
}
}
})
#profile {
position: relative;
display: inline-block;
margin-left:100px;
margin-top:50px;
padding: 10px;
background-color: red;
}
<div id="profile">My Profile</div>
当您将鼠标悬停在红色父框上,然后没有将鼠标悬停在蓝色子框上时,您只需将鼠标悬停在父蓝色框外,日志显示如下:
HTMLCollection [div#miniProfile, miniProfile: div#miniProfile]
但是当你打开它时,长度属性显示为 0。这是一个快照:
为什么?
另外,如果你将鼠标悬停在子蓝色框上,你会生成很多新的子元素,然后当你悬停时,你会得到一个 HTML 集合,里面有很多元素,但长度几乎总是接近实际的一半元素。
这是一个快照:
..再次,为什么?
解决方案
推荐阅读
- firebase - 只有具有读取权限的用户才能访问 Firebase 存储图像 URL
- typescript - 编译导出以供导入和窗口使用
- reactjs - 如何根据错误和触摸状态设置 Formik 字段的样式
- python-3.x - 如何自省 PEP 484 类型提示?
- python - Python:hyp2f1 与 sympy 的符号使用
- reactjs - Codesandbox 为 React 应用程序提供“目标容器不是 DOM 元素”错误,但 webpack-dev-server 没有
- c# - 使用函数连接到本地 sql db 时键“attachdbfilename”的值无效
- python - 在 itertools.permutations 中将范围设置为参数
- mysql - MYSQL CONV 从十六进制到十进制问题
- jquery - 即使在 html 中生成,Kendo.Combobox 也不显示其选项