错误:无法使用jQuery读取未定义的属性'defaultView'

时间:2011-12-08 09:45:55

标签: javascript jquery

我有这段代码

$(document).ready(function (){
    var Form_addTurbo = $('form#Form_addTurbo');
    Form_addTurbo.submit(function (e){
        e.preventDefault;
        v = new Notification('Creating Turbo.', 'information', 'Working..', function(){return;});
        $.post('/api/turbos/new.php', {
            action : 'createNew'
        }, function (r){
            v.hide();
            if(r.success){
                new Notification('Turbo Create.', 'saved', '', function(){return;});
            }else if(r.error){

            }else{
                new Notification('Something went wrong.', 'error', '', function(){return;});
            }
        }, 'json');
        return false;
    });
});

使用此api

$(document).ready(function(e) {$("body").prepend('<ul id="notifications"></ul>');});

/**
 * Global notification system
 *
 * @param  String      Message to be displayed
 * @param  String      Type of notification
 *
 * @author    Bram Jetten
 * @version    28-03-2011
 */
Notification.fn = Notification.prototype;

function Notification(value, type, tag, onclickfunc) {
  this.log(value, type);
  this.element = $('<li><span class="image '+ type +'"></span>' + value + '</li>');
  if(typeof tag !== "undefined" && tag !== '') {
    $(this.element).append('<span class="tag">' + tag + '</span>');
  }
  if(typeof onclickfunc == 'function'){
      this.element.click(onclickfunc);
  }
  $("#notifications").append(this.element);
  this.show();
}

/**
 * Show notification
 */
Notification.fn.show = function() {
  $(this.element).slideDown(200);
  $(this.element).click(this.hide);
}

/**
 * Hide notification
 */
Notification.fn.hide = function() {  
  $(this).animate({opacity: .01}, 200, function() {
    $(this).slideUp(200, function() {
      $(this).remove();
    });
  });
}

/**
 * Log notification
 * 
 * @param  String      Message to be logged
 * @param  String      Type of notification
 */
Notification.fn.log = function(value, type) {
  switch(type) {
    case "information":
      console.info("*** " + value + " ***");
      break;
    case "success":
      console.log(value);
      break;
    case "warning":
      console.warn(value);
      break;
    case "error":
      console.error(value);
      break;
    case "saved":
      console.log(value);
      break;
    default:
      console.log(value);
      break;
  }
}

所以会发生的事情是我尝试使用我认为它给出的隐藏功能关闭通知但是我收到了这个错误。

  

未捕获的TypeError:无法读取未定义的属性“defaultView”

我相信它不知道“这个”是什么,但我怎么能让它工作。

1 个答案:

答案 0 :(得分:0)

对您的示例进行了一些小改动。试一试http://jsfiddle.net/FLE39/

仔细看看你的代码并制作了一个新的jsfiddle。请参阅更新的链接。