2012-02-17 34 views
0

大家好,請讓我知道下面的語句JavaScript的HTML上5

addEvent(window, 'storage', function (event){ 
    if (event.key == 'storage-event-test'){ 
     output.innerHTML = event.newValue; 
    } 
}); 

addEvent(dataInput, 'keyup', function(){ 
    localStorage.setItem('storage-event-test', this.value); 
}); 

請解釋一下我的意思是什麼的addEvent()方法和做什麼呢上面的代碼。

回答

2

沒有addEvent方法作爲javascript的一部分,可能是一個外部編寫的函數。有element.addEventListener

你的代碼中的addEvent預計此簽名:

addEvent(obj, an_event_string, callback_fn); 

第一個參數,我不知道,只是需要一個對象。第二個是表示(我在猜測)事件的字符串,第三個是事件發生時調用的函數。

addEvent(window, 'storage', function (event){ 
    //for the "storage" event this function is called 
    // and some info is passed in the event argument 
    if (event.key == 'storage-event-test'){ //if the key is.. 
     output.innerHTML = event.newValue; 
    }//then set the innerHtml to a value from the event 
}); 

addEvent(dataInput, 'keyup', function(){ 
    //for the "keyup" event 
    //save an item into local storage 
    localStorage.setItem('storage-event-test', this.value); 
}); 

爲更多本地存儲見here