Blogger Information
Blog 54
fans 4
comment 1
visits 54823
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
h5新增的js选择器
神仙不在的博客
Original
1111 people have browsed it

document.getElementsByClassName('big');

返回是一个伪数组,根据类名来选择。

document.querySelector('.box');   css选择器

document.querySelector('#nav');   css选择器

document.querySelector('li');   css选择器

这个通杀,声明都能选,但是返回的是选中的第一个元素

document.querySelectorAll('box');   返回的是集合

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="box">盒子1</div>
<div class="box">盒子2</div>
<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
</ul>
<script>
    var box = document.getElementsByClassName('box');
    // 返回第一个元素
    var firstbox = document.querySelector('.box');
    console.log(firstbox);
</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post