获取document对象除了getElement方法,居然还可以使用css选择器方法!!!
2016-01-10 17:28
686 查看
例子:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>example</title>
<style type="text/css">
pre{
border: thin solid black;
}
</style>
</head>
<body>
<pre id="results"></pre>
<img id="caomei"src="img/4d0df8064383e78d74e07014e7ccd0ee.jpg" name="image" alt="caomei">
<p id="tblock">
There are lots of different kinds of fruit.There are over 500 varieties of <span id="banana">banana</span> alone.By the time we add the countless types of apples,oranges,and other well-known
fruit,we are faced with thousands of choices.
</p>
<img id="apple" src="img/63fddd61e1afe42e38c6ce76910be815.jpg" name="image" alt="apple">
<p>
One of the most interesting aspects of fruit is the variety available in each country.I live near London,in an area which is known for its apples.
</p>
<img id="san" src="img/161026fa577b8f3aa88187bd67b546fd.jpg" alt="san" />
<script>
var resultsElement=document.getElementById("results");
var elems=document.getElementById("tblock").getElementsByTagName("span");
resultsElement.innerHTML+="there are "+elems.length+" span elements(getelement方法)\n";
var elems2=document.getElementById("tblock").querySelectorAll("span");
resultsElement.innerHTML+="there are "+elems2.length+" span elements(getelement+css方法)\n";
var elems3=document.querySelectorAll("#tblock>span");
resultsElement.innerHTML+="there are "+elems3.length+" span elements(css选择器方法)\n";
</script>
</body>
结果:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>example</title>
<style type="text/css">
pre{
border: thin solid black;
}
</style>
</head>
<body>
<pre id="results"></pre>
<img id="caomei"src="img/4d0df8064383e78d74e07014e7ccd0ee.jpg" name="image" alt="caomei">
<p id="tblock">
There are lots of different kinds of fruit.There are over 500 varieties of <span id="banana">banana</span> alone.By the time we add the countless types of apples,oranges,and other well-known
fruit,we are faced with thousands of choices.
</p>
<img id="apple" src="img/63fddd61e1afe42e38c6ce76910be815.jpg" name="image" alt="apple">
<p>
One of the most interesting aspects of fruit is the variety available in each country.I live near London,in an area which is known for its apples.
</p>
<img id="san" src="img/161026fa577b8f3aa88187bd67b546fd.jpg" alt="san" />
<script>
var resultsElement=document.getElementById("results");
var elems=document.getElementById("tblock").getElementsByTagName("span");
resultsElement.innerHTML+="there are "+elems.length+" span elements(getelement方法)\n";
var elems2=document.getElementById("tblock").querySelectorAll("span");
resultsElement.innerHTML+="there are "+elems2.length+" span elements(getelement+css方法)\n";
var elems3=document.querySelectorAll("#tblock>span");
resultsElement.innerHTML+="there are "+elems3.length+" span elements(css选择器方法)\n";
</script>
</body>
结果:
there are 1 span elements(getelement方法)
there are 1 span elements(getelement+css方法)
there are 1 span elements(css选择器方法)
相关文章推荐
- CSS权威指南学习笔记(1)
- 简易在线投票系统(php)——前端设计(css)
- 利用css3伪类实现边框环绕效果
- css3制作立体导航
- 【CSS3】CSS3 滤镜实现
- 燕十八视频--CSS
- 燕十八---CSS画三角形
- css3实现图片旋转效果
- css中margin、padding等属性的简写方式解读
- 初学DIV+CSS需要注意的6个问题
- 如何书写可维护的CSS代码
- CSS代码书写顺序
- css中margin、padding等属性的简写方式解读
- 初学DIV+CSS需要注意的6个问题
- 如何书写可维护的CSS代码
- CSS代码书写顺序
- css display inline block 兼容性问题写法
- CSS 弹性布局如何实现
- 缩短浏览器上页面渲染
- CSS-Day02-样式表的分类