【问题标题】:jGrowl: with a id inside function, won't work, only if no id?jGrowl:只有在没有 id 的情况下,函数内部有 id 才能工作?
【发布时间】:2012-01-15 23:00:22
【问题描述】:

在一个普通的 JS 函数中:

$('#friendsPop').jGrowl("testtmee");

不会工作,但是:

$.jGrowl("testmeee");

工作得很好..我已经测试了所有内容,如果我在函数外部创建链接,正常的 a: 链接如下:

<a href="javascript:void(0);" onclick="$('#friendsPop').jGrowl('testme');">link</a>

它也可以正常工作。但我想激活

$('#friendsPop').jGrowl("testtmee");

在 ajax 成功调用之后,我需要一个 ID。

我该怎么办?

【问题讨论】:

    标签: jquery jgrowl


    【解决方案1】:

    这是我的建议。由于没有办法将 jGrowl 放在 DIV 上,无论是向左还是向右浮动,我们将改为在渲染后传输它。下面是我从原始示例中获取的代码。

    (function($){
        $.jGrowl.defaults.pool = 5;
        $.jGrowl.defaults.sticky = true;
        $.jGrowl.defaults.life = 2500;
        $.jGrowl.defaults.closer = true;
        $.jGrowl.defaults.open = function(e,m,o) {$("#jGrowl").css('right', $('#header').offset().left + 17);};
    
        /**
         * @todo Add the twitter avatars to tweets, via tweet.user["profile_image_url"]
         * @todo Find a way to calculate the dates relatively
         * @todo Test is a cookie plugin is available so that some of this data can be 'cached'
         */
        $.jTweet = function( username , total ) {
            $.getJSON("http://twitter.com/status/user_timeline/" + username + ".json?count=" + total + "&callback=?", function(response) { 
                $.each(response, function(i, tweet) {
                    $.jGrowl( (tweet.text.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
                        return m.link(m);
                    })) + ' [' + tweet.source + ']' , { 
                        header: tweet.created_at , 
                        } );
                });
            });
        };
    
        $.jTweet('jquery', 5);
    })(jQuery);
    

    这里的重要部分是行:

    $.jGrowl.defaults.open = function(e,m,o) {$("#jGrowl").css('right', $('#header').offset().left + 17);};
    

    这将 jGrowl 对象的 right 属性与我的 header 的 left 属性对齐。这基本上转移了它的位置,使它看起来好像在标题的 div 下。

    【讨论】:

      【解决方案2】:

      您必须将此添加到您的 ajax 请求中:

      var mesScripts = document.getElementById("mapleft").getElementsByTagName("script");
      for (var i=0; i<mesScripts.length; i++) {
          eval(mesScripts[i].innerHTML);
      

      在 ajax 加载后,它会修改 &lt;script&gt;

      【讨论】:

        【解决方案3】:

        我经常使用 jGrowl,但我不记得曾经在一组选定的 jQuery 对象上调用它。 jGrowl 的全部意义在于它在窗口级别生成“未附加的”消息——一个单一的全局消息流,不管它们是如何被调用的(或从哪里调用的)。就像它所基于的 Mac OS X Growl 概念一样。

        查看jGrowl documentation,我也没有看到任何这样调用它的示例:

        $('.something').jGrowl('message'); 
        // what would this even mean in jGrowl terms?
        

        总是这样:

        $.jGrowl('message');
        

        如果它有时以这种方式为您工作,那是偶然的(我没有查看 jGrowl 源代码来找出原因,但我确信答案就在那里)。

        只需调用它$.jGrowl('Like This') 就可以了。

        【讨论】:

        • 嗨,肯。我需要指定一个#id,所以在我的另一个 ajax 调用成功时我可以做 $('#something').jGrowl('close');.. 这就是为什么我希望它工作它并不总是这样,如果例如,您下载 jgrowl,在示例目录的 jquery.html 中,您会找到自定义容器 $('#test1').jGrowl('message');等等,然后在 body close 之前你会发现
          所以它会显示在特定位置(该示例的左下角)。
        • Karem,你能设置一个显示问题的可访问页面吗?
        猜你喜欢
        • 2018-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-17
        相关资源
        最近更新 更多