javascript - TypeError: undefined 不是 "indexOf"上的函数?

标签 javascript typeerror indexof

在我的 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/

相关文章:

python - TypeError: 无法在 python 的 SCRAPY 中创建对 'str' 对象的弱引用

Javascript 错误停止 $document.ready()

特殊波斯字符的 C# IndexOf

javascript - 为什么 Array.indexOf 找不到相同的对象

c# - 什么是可以处理代理对的 String.IndexOf(string input) 的 Unicode 安全副本?

javascript - 如何在前端使用 JavaScript Fetch API 从 FastAPI 后端下载文件?

javascript - 为什么我无法在 javascript 中解析 xml?

javascript - 由于 php 中的 js 文件,提交按钮不起作用

javascript - 为 webworkers 中的导入脚本指定字符集

python - TypeError: 'list' 对象不能被解释为整数