我如何使用/追加pnotify到div

时间:2014-04-02 19:26:59

标签: javascript jquery html pnotify

我正在使用 Pnotify 发送通知,但通知仅在非常具体的位置显示。我想将它们附加到我页面中的标题类中,但我不知道该怎么做。

JS:

function showNotify(data){
    var notice = $.pnotify({
        type: 'success',
        delay: 300000,
        addclass: (isDevice)? 'body-device stack-topleft':'stack-topleft',
        mouse_reset: false
    }).click(function(e){
        notice.pnotify_remove();
    });
}

我想要附加我的pnotify的HTML是:

<div class="header">
    <div class="col-md-12" data-bind="with: activeRoute"></div>
</div>

2 个答案:

答案 0 :(得分:1)

更新以解决如何将pnotify附加到标题类

在我重新阅读您的问题之后,我假设您可能正在寻找类似 updated sample 的内容。在数据属性下方提供了pnotify消息的标题和文字。

<div class="header" 
          data-title="notify 1" 
          data-text="text 1">Header with autowiring of pnotify</div>

<div class="header" 
          data-title="better notify 2" 
          data-text="other text 2">Header 2</div>

要使用标题类连接pnotify,您可以添加以下代码:

 function getNotifiyMessage(that){      
      var mytitle = $(that).attr("data-title" );
      var mytext = $(that).attr("data-text");          
      return { title: mytitle, text: mytext};
    };

    $(document).ready(function() {

          $(".header").click(
            function(){
              var notifyMessage = getNotifiyMessage(this);
              $.pnotify(notifyMessage);
            }
          );
    });    

较早的第一个答案

如果你mean this pnotify基于样本

<button class="btn btn-default source" 
      onclick="$.pnotify({ title: 'Regular Notice'
            , text: 'i am a note from pnotify'});"
  >Regular Notice</button>

您可以通过添加具有激活pnotify功能的onclick属性来创建pnotify。看到 this sample data-attribute下面提供了pnotify消息的标题和文本。

<div class="header" 
  onclick="$.pnotify({ title: 'Regular Notice', text: 'i am a note from pnotify'});">
  <div class="col-md-12" 
       data-bind="with: activeRoute">go click me to see pnotify</div>
</div>

请注意,事件上方从内部div到外部div冒泡,然后pnotify触发。

答案 1 :(得分:1)

来自http://sciactive.com/pnotify/#demos-simple

function show_stack_context(type) {
    if (typeof stack_context === "undefined") stack_context = {
        "dir1": "down",
        "dir2": "left",
        "context": $("#stack-context")
    };
    var opts = {
        title: "Over Here",
        text: "Check me out. I'm in a different stack.",
        stack: stack_context
    };
    switch (type) {
    case 'error':
        opts.title = "Oh No";
        opts.text = "Watch out for that water tower!";
        opts.type = "error";
        break;
    case 'info':
        opts.title = "Breaking News";
        opts.text = "Have you met Ted?";
        opts.type = "info";
        break;
    case 'success':
        opts.title = "Good News Everyone";
        opts.text = "I've invented a device that bites shiny metal asses.";
        opts.type = "success";
        break;
    }
    $.pnotify(opts);
}

所以你会

function showNotify(data){
    if (typeof stack_context === "undefined") stack_context = {
        "dir1": "down",
        "dir2": "left",
        "context": $(".header")
    };
    var opts = {
        title: "Over Here",
        text: "Notification",
        type: 'success',
        delay: 300000,
        addclass: (isDevice)? 'body-device stack-topleft':'stack-topleft',
        mouse_reset: false
        stack: stack_context
    };
    var notice = $.pnotify(opts).click(function(e){
        notice.pnotify_remove();
    });
}