jQuery通知栏

时间:2012-12-04 21:51:15

标签: javascript jquery html notifications

我正在尝试在结帐页面(FoxyCart模板)上安装Noty(jQuery Notification插件)。我在结帐模板的部分中安装了以下代码:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

<script type="text/javascript" src="http://lavajavamaui.com/js/noty/jquery.noty.js"></script>

<script type="text/javascript" src="http://lavajavamaui.com/js/noty/layouts/top.js"></script>
<script type="text/javascript" src="http://lavajavamaui.com/js/noty/layouts/topLeft.js"></script>
<script type="text/javascript" src="http://lavajavamaui.com/js/noty/layouts/topRight.js"></script>
<!-- You can add more layouts if you want -->

<script type="text/javascript" src="http://lavajavamaui.com/js/noty/themes/default.js">
</script>
<script type="text/javascript">
var noty = noty({text: 'noty - a jquery notification library!'});
</script>

如果我理解正确,这就是为了让它正确显示所需要做的一切。出于某种原因,通知不会弹出。这条线有问题吗?

<script type="text/javascript">
var noty = noty({text: 'noty - a jquery notification library!'});
</script>

这里应该安装Noty the page

1 个答案:

答案 0 :(得分:1)

您正在复制文档中的错误。这条线

var noty = noty({text: 'noty - a jquery notification library!'});

如果在全局上下文中运行,则重新定义noty,将函数noty替换为noty函数的结果。从理论上讲,第一次调用应该有效,但是对noty的任何后续调用都会失败,因为它们试图将对象作为函数调用。我不确定为什么第一个失败,但它可能是相关的。尝试将要分配结果的对象的名称更改为:

var notyObj = noty({text: 'noty - a jquery notification library!'});

这可能会解决它。

如果您在创建通知后不需要操作通知,那么您也应该能够在不将结果分配给变量的情况下调用它:

noty({text: 'noty - a jquery notification library!'});

<强>更新

如果在非全局上下文(例如jQuery的ready事件)中执行该语句,则由于variable hoisting而无效。提升采用任何var语句并将其移至语句的顶部。声明:

    (function () {
        var noty = noty({text: 'noty - a jquery notification library!'});
    }());

变成了:

    (function () {
        var noty; // inside this function noty now refers
                  // to a local variable set to "undefined"

        // JavaScript looks for local variables before it looks at globals,
        // so it will use the new local "noty" when it tries to execute the
        // right side of this line. This will fail because you
        // can't invoke undefined
        noty = noty({text: 'noty - a jquery notification library!'});
    }());