2017-06-01 113 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 –