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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8428985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:25:07  来源:igfitidea点击:

Error: Cannot read property 'defaultView' of undefined, using jQuery

javascriptjquery

提问by Bradley Weston

I have this code

我有这个代码

$(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;
    });
});

Which uses this api

哪个使用这个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;
  }
}

So what happens is that I try close the notification using the hide function that I believe it gives but I get this error.

所以发生的事情是我尝试使用我认为它提供的隐藏功能关闭通知,但我收到此错误。

Uncaught TypeError: Cannot read property 'defaultView' of undefined

未捕获的类型错误:无法读取未定义的属性“defaultView”

I believe it doesn't know what "this" is but how would I be able to get this to work.

我相信它不知道“这个”是什么,但我如何才能让它发挥作用。

采纳答案by Stefan

Made some minor changes to your example. Try it out http://jsfiddle.net/FLE39/

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

Had a closer look at your code and made a new jsfiddle. See updated link.

仔细查看您的代码并制作了一个新的 jsfiddle。查看更新的链接。