2017-02-27 60 views
0

我正在使用jsdom創建window.document對象並將其傳遞到正在測試的函數中。然而,創建一個新元素後,我不能設置dataset屬性:jsdom - 如何設置元素數據集?

var my = function(document){ 
    var item = document.createElement('a'); 
    gridItem.tabIndex = 1; 
    gridItem.dataset.num = 1; 
}; 

運行我的測試時,我得到的錯誤是:

TypeError: Cannot set property 'num' of undefined

回答

0

用途:

element.setAttribute("data-foo","bar"); 

,而不是

element.dataset.foo = "bar";