在我的 DOM 中爬行,我发现了这个令人头疼的问题:
我正在评估具有以下分配的元素:
if (e.container.context.classList.indexOf('editable-notes') == - 1) return;
我的开发人员工具包告诉我这应该可行……我想。但它爆炸了:
Uncaught TypeError: undefined is not a function.
...这对我来说似乎很奇怪。所以我设置了一个断点,然后检查这些值。 e.container.context
返回为有效:
<td title class="truncate editable-notes k-edit-cell" role="gridcell" id="partner-list_active_cell" data-role="editable">
<input class="k-textbox" data-val="true" data-val-length="The field Notes must be a string with a maximum length of 500." data-val-length-max="500" id="Notes" name="Notes" type="text" value data-bind="value:Notes">
<span class="field-validation-valid" data-valmsg-for="Notes" data-valmsg-replace="true" style="display: none;"></span>
</td>
...到目前为止,还不错。 e.container.context.classList
也返回一个有效值:
["truncate", "editable-notes", "k-edit-cell"]
...e.container.context.classList.indexOf('editable-notes')
的评估失败了:
TypeError: undefined is not a function
message: "undefined is not a function"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error
... 所以现在我很困惑。我认为 indexOf 几乎得到了普遍支持。我在做傻事吗?
最佳答案
classList
属性包含 DOMTokenList
,它们不是数组并且没有 indexOf
。他们所拥有的是a method准确地做你需要的,但更直接:
if (!e.container.context.classList.contains('editable-notes'))
return;
关于javascript - TypeError: undefined 不是 "indexOf"上的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24536458/