2011-12-08 21 views
1

我有這樣的代碼錯誤:無法讀取的未定義的屬性「默認視圖」,使用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; 
    } 
} 

那麼,什麼情況是,我嘗試使用關閉的通知隱藏功能,我相信它給,但我得到這個錯誤。

Uncaught TypeError: Cannot read property 'defaultView' of undefined

我相信它不知道「this」是什麼,但我怎麼能得到這個工作。

+1

已包含在你的HTML'' jQuery的標籤JS文件?附:我沒有downvote ... – ManseUK

+1

你可以創建一個測試用例,例如:在http://jsfiddle.net/?你的錯誤似乎源於jQuery框架。 –

+0

我剛剛創建了一個URL的jsfiddle http://jsfiddle.net/aF4TQ/1/希望你能幫助:) –

回答

0

做出你的榜樣一些細微的變化。試試吧http://jsfiddle.net/FLE39/

曾在你的代碼仔細看看,並提出了新的jsfiddle。查看更新的鏈接。

+0

它仍然不會隱藏。 這是新的錯誤 未捕獲的SyntaxError:意外的標識 ,我發現了是因爲$(這)一個.remove(); –

+0

哦,它被放置在jQuery準備好了。 –

+0

這部分'$(「#notifications」)。append(this.element);'將不會工作,直到DOM完成加載並添加了#notifications元素。 – Stefan

相關問題