在js中,可以使用getElementsByClassName方法获得同一类名的多稍僚敉视个input对象。下面小编举例讲解js如何通过类名获得多个input对象。
工具/原料
html+js
代码编辑器:zendstudio10.0
方法/步骤
1、新建一个html文件,命名为test.html,用于讲解js如何通过类名获得多个input对象。
2、在test.html文件内,使用input标签创建多个文本框,用于测试。
3、在test.html文件内,设置input标签的class属性为abc。
4、在js标签内,使用getElementsByClassName方法获得input对象。
5、在js标签内,再通过for循环遍历input对象,将每一个input的背景颜色设置为灰色。
6、在浏览器打开test.html文件,查看结果。