gpt4 book ai didi

javascript - Chrome 找不到 offsetTop

转载 作者:行者123 更新时间:2023-11-30 18:43:35 25 4
gpt4 key购买 nike

style.top offsetTop 在 chrome 中是 'object DOM Window',它没有给出整数,为什么。我用谷歌搜索了这样的问题,但似乎只有我一个人有这样的问题。

它们在 Firefox 中运行良好。如何在 Chrome 中获取元素的 offsetTop。

代码:

<input type="button"id='test' value="clickme"/>
<script>
document.getElementById('test').addEventListener('click',function(){
alert(this.offsetTop);
top = this.offsetTop;
alert(top);
},true)
</script>

嗯,很有意思,在alert功能中起作用,但是不能存储。

最佳答案

因为 window.top 已经是浏览器的内置属性。看这里:https://developer.mozilla.org/en/DOM/window.top

这个属性是只读的(see here):

readonly attribute WindowProxy top;

这意味着赋值 top = this.offsetTop 没有做任何事情。

这是由于全局 namespace 污染而出现的问题的典型案例。不要这样做。相反,在本地声明您的变量:

document.getElementById('test').addEventListener('click', function() {
var top = this.offsetTop;
alert(top);
}, true);

关于javascript - Chrome 找不到 offsetTop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6122698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com