Blogger Information
Blog 7
fans 0
comment 0
visits 5093
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
原生querySelector()与querySelectorAll()获取元素实例
phpcn_u68463的博客
Original
807 people have browsed it

<html>

<head>

<meta charset="UTF-8">

<title>css方法获取页面元素</title>

</head>

<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>

<body>

<ul>

<li>7:00起床</li>

<li>7:30出门上班</li>

<li>8:50下地铁</li>

<li>9:00吃早餐</li>

<li>9:30到公司打卡上班</li>

</ul>

<button>点击改变</button>

</body>

</html>

<script type="text/javascript">

var btn = document.getElementsByTagName('button')[0]



// 1.获取单个的方法

btn.onclick = function(){

document.querySelector('li').style.background = 'blue'


}



// 2.获取所有的方法

btn.onclick = function(){


var li =document.getElementsByTagName('li')

var li =document.querySelectorAll('li')

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

li[i].style.background = 'pink'

}

}

</script>

2017-12-24_192704.png

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!