JAVAEE——JavaScript四种常见的选择器

<body>

<input type="text" id="inp" value="123" class="a13" />

<input type="text" class="in" name="name"/>

<input type="text" class="in" name="name"/>

<input type="text" class="in" />

<input type="text" class="in" />

<script>

id选择器

var inp = document.getElementById("inp");

console.log(inp);

class选择器(获取到的是数组,需要for循环遍历)

var inp = document.getElementsByClassName("in");

for(var i = 0; i < inp.length; i++){

console.log(inp[i]);

}

name选择器(获取到的是数组,需要for循环遍历)

var inp = document.getElementsByName("name");

for(var i = 0; i < inp.length; i++){

console.log(inp[i]);

}

标签选择器(获取到的是数组,需要for循环遍历)

var inp = document.getElementsByTagName("input");

for(var i = 0; i < inp.length; i++){

console.log(inp[i]);

}

</script>

</body>

日常开发中ID选择器和Class选择器用的比较多

相关推荐
用户1456775610371 天前
干净的图片批量处理,处理速度飞快
前端
用户1456775610371 天前
亲测好用!简单实用的图片尺寸调整工具
前端
索西引擎1 天前
npm、yarn、pnpm
前端·npm·node.js
天生我材必有用_吴用1 天前
Vue3 + VitePress 搭建组件库文档平台(结合 Element Plus 与 Arco Design Vue)—— 超详细图文教程
前端
liu****1 天前
基于websocket的多用户网页五子棋(八)
服务器·前端·javascript·数据库·c++·websocket·个人开发
San301 天前
深入理解 JavaScript 函数:从基础到高阶应用
前端·javascript·node.js
ttyyttemo1 天前
Column,rememberScrollState,记住滚动位置
前端
芒果茶叶1 天前
并行SSR,SSR并行加载
前端·javascript·架构
vortex51 天前
解决 Kali 中 Firefox 下载语言包和插件速度慢的问题:配置国内镜像加速
前端·firefox·腾讯云
修仙的人1 天前
Rust + WebAssembly 实战!别再听说,学会使用!
前端·rust