2017-06-01 126 views
0

似乎在Internet Explorer中使用element.dataset時出現問題。數據集和Internet Explorer 11

收到此錯誤

無法獲取屬性 '菜單' 的未定義或空引用

這顯示在下面的代碼行:

if (!node instanceof HTMLElement || !node.dataset.menu) { 

- -------------------------------------

如果我使用谷歌瀏覽器並輸入以下命令:

document.querySelectorAll( '[數據菜單]')

我得到如下:

enter image description here

但是,如果我在Internet Explorer 11中運行相同的命令:

enter image description here

它似乎找到了2 [data-menu]屬性,但是沒有列出它們,所以NodeList是空的,因此我的錯誤。

任何人都知道這個問題的解決方案?

編輯

經過進一步調查,但看來IE是生成與所述data-menu屬性的匹配元素的列表節點列表,但是它不會出現它們可使用dataset像任何其他瀏覽器是訪問將。

解決方案?

+0

您是否嘗試過使用'的getAttribute()'? IE 11應該支持'dataset',但是嘗試使用11-後備。您是否啓用了IE兼容模式? – Pyromonk

+0

問題是我使用querySelector來獲取我所需要的元素的列表,我將不得不引用元素的一些其他方式來使用getAttribute。 –

+0

['.forEach.call(document.querySelectorAll('[data-menu]'),function(value,index,array){/*...*/})'](https:// developer。如果我理解你的話,mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach?v = control)可能會有所幫助。 – Pyromonk

回答

0

你可以簡單的改變if (!node instanceof HTMLElement || !node.dataset.menu)if (!node instanceof HTMLElement || !node.dataset || !node.dataset.menu)

+0

這不會解決問題,它需要檢查數據集中是否有菜單。問題出在Internet Explorer和數據集上,你可以看到我把下面的命令看看它包含了什麼,它說undefined:S –