javascript - 如何使用 dojo 从 innerText 或 InnerHTML 获取字符数?
问题描述
我必须Dojo
在我的应用程序中使用,我试图在一个元素中获取总数HTML
,但我一直收到错误消息。
这是我的代码:
var attributeIcons = dojo.query(".attribute-icon");
if (attributeIcons.innerText.length = 4) {
console.log(attributeIcons);
}
我也尝试使用这种方法:
var attributeIcons = document.getElementsByClassName("attribute-icon").innerHTML.length;
console.log(attributeIcons);
每种方法都给我同样的错误:
Uncaught TypeError: Cannot read property 'length' of undefined
解决方案
dojo.query() 和 document.getElementsByClassName() 都返回一个类似数组的对象。这意味着你不能在节点数组上调用 .innerHTML(你得到未定义的),随后你也不能调用 .length。
查看这两个参考: https://dojotoolkit.org/reference-guide/1.7/dojo/query.html, https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName
尝试运行以下命令以查看您的阵列。
var attributeIcons = dojo.query(".attribute-icon");
console.log(attributeIcons)
// or
var attributeIcons = document.getElementsByClassName("attribute-icon");
console.log(attributeIcons)
您可以选择其中一个数组项,然后在其上运行 .innerHTML.length,而不是在整个数组上运行。
var attributeIcons = dojo.query(".attribute-icon");
console.log(attributeIcons[0].innerHTML.length)
// or
var attributeIcons = document.getElementsByClassName("attribute-icon");
console.log(attributeIcons[0].innerHTML.length)
希望有帮助!
推荐阅读
- javascript - 类继承和构造函数
- azure-data-factory-2 - 如何在 Azure 数据工厂数据流 JSON 接收器中设置日期格式?
- google-sheets - 如何根据 Google Sheet 中的其他列排列行
- java - 如果我想拥有一个工厂,如何实现这些类?
- python - 我的模型字段未显示在管理面板中
- excel - Excel:如果某个单元格不为空,则将一行复制到另一个工作表
- python - 如何在 Python 中将列表值插入到字典中?
- python - 在后台运行多行子进程命令
- c# - Xunit - Parameterized Tests - Class Properties - C#
- c# - 如何使 `Func<>` 的一些参数可选?