"getElementsByClassName is not a function" 报错原因
element.getElementsByClassName(""):
返回的含有该类的子元素数组,除了子元素以外的后代元素是获取不到的。要遍历使用,或者.element.getElementsByClassName("")[0]; 这样的方式获取元素。
document.getElementsByClassName:
这个才会返回含有该类的后代元素。
见MDN对element.getElementsByClassName()的定义:
中文版:
Element.getElementsByClassName()
方法返回一个即时更新的(live) HTMLCollection
,包含了所有拥有指定 class 的子元素。
当在 document 对象上调用此方法时,会检索整个文档,包括根元素。
相似地,getElementsByClassName()
方法会在整个文档上执行;它返回指定拥有指定 class 名称的 document 根节点的后代元素。
英文版本:
The Element
method getElementsByClassName()
returns a live HTMLCollection
which contains every descendant element which has the specified class name or names.
The method getElementsByClassName()
on the Document
interface works essentially the same way, except it acts on the entire document, starting at the document root.
注:英文版中的descendant是后代的意思,也就是说element.getElementsByClassName("");获取的含有该类名的后代元素,可是这样会报错。
西班牙语版:
El método Element.getElementsByClassName()
retorna una HTMLCollection
"viva" conteniendo todos los elementos hijos que tienen todos los nombres de clase dados. Cuando se llama en el objeto document, se realiza la búsqueda en el documento completo, incluyendo el nodo raíz.
De forma similar, el método Document.getElementsByClassName()
actúa en todo el documento; retornará los elementos que sean descendientes del documento raiz especificado que tengan los nombres de clase dados.
注:hijos在西语中意思是儿子,返回的是子元素,descendientes,后代的意思。
法语版:
La méthode Element.getElementsByClassName()
retourne une HTMLCollection
contenant une référence sur tous les éléments ayant les noms de classes passés en paramètre. Quand la méthode est appelée sur l'objet document, la recherche s'effectue dans le document complet, noeud racine compris.
À l'instar de la méthode Document.getElementsByClassName()
qui agit sur le document entier ; elle va retourner les descendants de l'élément spécifié comme document root qui auront les noms de classes passés en paramètre.
注: tous les éléments ayant,全部的含该类名的元素。也没有说明是子元素还是后代元素,传说中法语得的严谨呢?
日语版:
The Element.getElementsByClassName()
メソッドは、引数で与えられたクラス名を含む全ての 子要素を ライブな HTMLCollection
で返します。 documentオブジェクトで呼ばれたとき、ルートノートを含む全ドキュメントが検索されます。
同様に、Document.getElementsByClassName()
メソッドはドキュメント全体に働きます。特定されたドキュメントルート要素の子孫のうち、与えられたクラス名に合う複数のelementを返します。
注:日语也指明了是子元素。
俄语版:
Element.getElementsByClassName()
метод возвращает объект HTMLCollection
, содержащий в себе все дочерние элементы, которые имеют заданные имена классов. Если вызван на объекте document, будут возвращены все элементы, содержащиеся в документе.
Так же, как метод Document.getElementsByClassName()
действует на весь документ; это вернет элементы, которые являются потомками корневого элемента, содержащие в себе указанные классы.
注:дочерние элементы 谷歌把它翻译为child node子元素而不是descandants后代元素。
总结:
五局三胜
Element.getElementsByClassName() 返回的是含有该类名的所有子元素集合。
Document.getElementsByClassName() 返回的是含有该类名的后代元素集合。
"getElementsByClassName is not a function" 报错原因的更多相关文章
- VMware报错“原因: 未能锁定文件”,打开失败
原文:http://jingyan.baidu.com/article/425e69e6bf64dbbe15fc16fe.html VMware打开复制的虚拟机,报错“原因: 未能锁定文件”,打开失败 ...
- 查看报错原因 sshd -t
b for ssh.service failed because the control process exited with error code. See "systemctl sta ...
- @Autowired报错原因分析和4种解决方案!
上图的报错信息相信大部分程序员都遇到过,奇怪的是虽然代码报错,但丝毫不影响程序的正常执行,也就是虽然编译器 IDEA 报错,但程序却能正常的执行,那这其中的原因又是为何? 报错原因分析 报错的原因 ...
- Error: Cannot find module 'webpack/bin/config-yargs' 报错原因, webpack@4.X踩的坑~
1 .使用webpack@4.32.2时, 当我通过package.json的script去执行webpack-dev-server时, 报以下错误: Error: Cannot find modu ...
- angularJS中$http.get( ).success( )报错原因及解决方案
一.问题描述: 电脑安装的angular1.6.7版本,项目中使用了$http.get( ).success( ),控制台报错: $http.get(...).success is not a fun ...
- MySQL server has gone away报错原因分析/
在平时和开发的交流 以及 在论坛回答问题的或称中会发现这个问题被问及的频率非常高. 程序中报错: MySQL server has gone away 是什么意思? 如何避免? 因此,感觉有必要总结一 ...
- Java-maven异常-cannot be cast to javax.servlet.Filter 报错, 原因servlet-api.jar冲突
使用maven开发web应用程序, 启动的时候报错: jar not loaded. See Servlet Spec . Offending class: javax/servlet/Servlet ...
- Spring JDBC保存枚举对象含关键字报错原因之一
报错信息: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized S ...
- 黄聪:Wordpress、PHP使用POST数据过大导致MySQL server has gone away报错原因分析
错误原因: 当POST的数据超过 max_allowed_packet 就会报 MySQL server has gone away 的错误. 1.查看当前Mysql的 max_allowed_pac ...
随机推荐
- 国庆 day 7 下午
思路:见博客. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...
- mysql-数据分组
一.创建分组 上面所讲的语句都是建立在表的所有数据或匹配特定的where子句的数据上进行的.是否能够进行分组,在进行汇总计算哪儿?例如:要想返回每个供应商提供的产品数目怎么办? 分组是在select语 ...
- LINUX 代码运行时间计算
clock_gettime比gettimeofday更加精确简单做了一下测试 #include<time.h>#include<stdio.h> #define MILLION ...
- HD-ACM算法专攻系列(3)——Least Common Multiple
题目描述: 源码: /**/ #include"iostream" using namespace std; int MinComMultiple(int n, int m) { ...
- 使用U盘安装win8(win8.1)系统
下载镜像 下载系统镜像,可以选32位或64位.下完后放到非系统盘(如果需要重新分区的,要先保存到移动硬盘或U盘) 使用U盘制作PE 准备一个4G以上的U盘(备份好U盘里的资料,因为刻录PE需要格式化U ...
- css inline-block列表布局
一.使用inline-block布局 二.多列布局方法二 <html><head> <meta charset="utf-8"> <tit ...
- 在 Ubuntu 18.04 LTS 无头服务器上安装 Oracle VirtualBox
作者: Sk 译者: LCTT qhwdw | 2018-10-12 01:59 本教程将指导你在 Ubuntu 18.04 LTS 无头服务器上,一步一步地安装 Oracle VirtualBox. ...
- JavaScript设计模式(biaoyansu)
1.构造器模式——创建类模式 ES6:class Student{ constructor(score,quality){ this.score = score this.quality = qu ...
- NodeJS学习笔记 (23)模块机制-module
https://github.com/chyingp/nodejs-learning-guide
- luogu P3157 [CQOI2011]动态逆序对(CDQ分治)
题目描述 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序 ...