JavaScript - 未捕获的类型错误 : Failed to execute 'getComputedStyle' on 'Window' : parameter 1 is not of type 'Element'

标签 javascript html

我收到以下错误:

Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'

我的 Stickyfill 源有问题 ( https://github.com/wilddeer/stickyfill )

我尝试了更多版本,但仍然遇到相同的错误。我的 JavaScript 源代码如下所示:

var a = document.getElementById('left');
Stickyfill.add(a);

为什么我会收到此错误,如何修复它?

编辑: 这是我的 html 页面:

    <!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="css-home.css">
        <script type="text/javascript" src="stickyfill.js"></script>
        <script type="text/javascript" src="js-home.js"></script>

    </head>
    <body>

        <div id="container">
            <div id="banner">
                <img src="sigla.jpg">
            </div>
            <div id="container2">
                <div id="left">
                    <nav>
                    <a target="ifr" href="">Home</a>
                    <a target="ifr" href="">Istoric</a>
                    <a target="ifr" href="echipa.html">Echipa</a>
                    <a target="ifr" href="">Galerie</a>
                    </nav>
                </div>
                <div id="right">

                    <iframe name="ifr" src="echipa.html" frameborder="0" ></iframe>

                </div>
            </div>
            <div id="footer">
                <p>CS MIOVENI</p>
            </div>
        </div>
    </body>

</html>

稍后编辑: 我已经使用 window.onload 我的 javascript 文件解决了这个问题

最佳答案

您能否确认:

var a = document.getElementById('left');

在将其传递给 Stickyfill.add(a) 之前实际上返回一个元素? 使用调试器单步执行或记录日志,例如;

var a = document.getElementById('left');
// Is an element returned?
console.log(a instanceof Element);
Stickyfill.add(a);

关于JavaScript - 未捕获的类型错误 : Failed to execute 'getComputedStyle' on 'Window' : parameter 1 is not of type 'Element' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40698912/

相关文章:

java - Android本地通知Cordova : Wait for cancelAll to finish

html - IE11 无法通过 Web 服务器显示带有对象标签的图像,但在本地可以正常显示

javascript - 使用 CSS/JS Firefox 问题翻转卡片

javascript - 单击时 2 个 div 之间的备用 z-index ?

javascript - 如何使用 Jquery 在特定时间后自动销毁 DOM 元素

javascript - 单击时在对象数组中追加新数据

html - CSS 相对位置与绝对位置

javascript - Angular JS 用户配置文件网格

javascript - 部分 html View 的 Controller 无法在使用 KendoPanelBar contenturl 的 Directive TemplateUrl 中工作

javascript - MIPS:将寄存器值打印到控制台