"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 ...
随机推荐
- Java 二进制,八进制,十进制,十六进制转换
A.十进制转换其他 十进制转成二进制 Integer.toBinaryString(int i) 十进制转成八进制 Integer.toOctalString(int i) 十进制转成十六进制: ...
- JConsole远程监控Tomcat7
下面技术应用于最优质的水果的鲜果篮 一.设置服务端: 1.增加Listener到conf/server.xml <Listener className="org.apache.cata ...
- 日期格式,Popup的使用方法,RenderTransform与LayoutTransform的区别
1.画个笑脸给大家娱乐一下: <Canvas Width="200" Height="180" VerticalAlignment="Cente ...
- 001.TypeScript简介.md
TypeScript是一门开源的,由微软开发维护的,发布于2012年10月的静态类型的语言: 他是ECMAScript的超集,支持JavaScript的所有语法和语义,并且在此基础之上提供了更多额外的 ...
- C# fixed语句固定变量详解
相信很多人在这样或那样的项目中,或者无意间看到了fixed语句块,看到之后你肯定会疑问: 1,这个fixed关键字是做什么用的? 2,什么情况下需要该关键字? 3,这个关键字该怎么用? 我相信解决了上 ...
- PostgreSQL Replication之第九章 与pgpool一起工作(5)
9.5 检查复制 如果所有的节点都处于开机并运行的状态.我们就可以在集群上运行我们的第一个操作了.在我们的例子中,我们将简单地连接到pgpool并创建一个新的数据库.createdb 是一个命令行工具 ...
- js字符串首字母转为大写
function initialsLetterUpperCase(arr){ if(Array.isArray(arr)){ return arr.map(function(val,index,arr ...
- 在Ubuntu上创建一个可以启动的U盘
1.概观 使用可启动的Ubuntu USB盘,您可以: 安装或升级Ubuntu 在不触及PC配置的情况下测试Ubuntu桌面体验 在借来的机器或网吧上启动到Ubuntu 使用USB盘上默认安装的工具来 ...
- react 中间件相关的一些源码解析
零.随便说说中间件 在react的使用中,我们可以将数据放到redux,甚至将一些数据相关的业务逻辑放到redux,这样可以简化我们组件,也更方便组件抽离.封装.复用,只是redux不能很好的处理异步 ...
- vscode 调试vue.js程序
npm install -g vue-cli //安装vue-clivue init webpack projectName //创建项目 1.Ctrl+~ 打开命令行 ...