【问题标题】:Using Tether Javascript Library causing exception使用 Tether Javascript 库导致异常
【发布时间】:2014-02-24 19:46:46
【问题描述】:

我正在开发一个新网站,我想在容器 div 的底部添加一个页脚。

我找到了一个名为 Tether (http://github.hubspot.com/tether/) 的 javascript 库,但我在实现时遇到了问题。

下面是我的 HTML 代码

<html>
    <head>
        <title>Bug Reporting</title>
        <link href="StyleSheet.css" type="text/css" rel="stylesheet" />
        <script src="includes/jquery.js"></script>
        <script src="includes/tether/tether.js"></script>
        <script>
            $(document).ready(function()
            {
                new Tether({
                    element: '.footer',
                    target: '.container',
                    attachment: 'top left',
                    targetAttachment: 'bottom left'
                });
            });
        </script>
    </head>
    <body>
        <div class="container">
            <header>
                This is the body
            </header>

            <div id="content">
                This is the content<br />
            </div>

            <div class="footer1">
                This is the footer
            </div>
        </div>
    </body>
</html>

下面是我的样式表

html, body
{
    font-family: arial;
    margin: 0;
    padding: 0;
}

header
{
    background-color: red;
    width: 100%;
    height: 80px;
}

.container
{
    background-color: yellow;
    height: calc(100% - 80px);
    width: 100%;
}

#content
{
    background-color: blue;
    width: 1024px;
    margin-left: auto;
    margin-right: auto;
    height: auto;
}

.footer1
{
    position: absolute;
    background-color: green;
    width: 100%;
    height: 80px;
}

问题是当我加载页面时,我在 tether 库中遇到异常

未捕获的类型错误:无法读取 null 的属性“classList”

感谢您提供的任何帮助。

【问题讨论】:

    标签: javascript html css tether


    【解决方案1】:

    Tether 的 optionselementtarget 定义为一个 DOM 或 jQuery 元素(在这个词的相当宽松的意义上),而不是一个字符串。

    您需要将一个 jQuery 对象传递给 Tether,如下所示:

    $(document).ready(function() {
        new Tether({
            element: $('.footer'),
            target: $('.container'),
            attachment: 'top left',
            targetAttachment: 'bottom left'
        });
    });
    

    在初始化 Tether 实例时,还要确保元素在页面上。

    【讨论】:

    • 谢谢,文档没有提到(反正我没有注意到),谢谢你的帮助
    • 没问题。我也对文档感到困惑。它们的措辞不是特别清楚,而且它们也在所有示例的下方
    • 截至 12 月 30 日,Tether 确实接受选择器字符串(例如 github.hubspot.com/tether/examples/simple)。刚刚更新了文档,感谢反馈。
    猜你喜欢
    • 2012-05-11
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多