获取document对象除了getElement方法,居然还可以使用css选择器方法!!!

本文探讨了不同国家丰富的水果种类,特别强调了苹果、香蕉等常见水果在英国的多样性。通过JavaScript代码展示了如何获取页面元素,进一步展示了信息技术在内容展示中的应用。

例子:

<!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选择器方法) 

1.以下哪个 HTML 标签用于定义文档的导航链接区域? A. <nav> B. <navigation> C. <navigate> D. <link> 2.在 CSS 中,哪个属性用于改变元素的字体大小? A. text-style B. font-size C. text-size D. font-style 3.如何用 JavaScript 选中 ID 为 "header" 的元素? A. document.getElement("header") B. document.querySelector("#header") C. document.getElementByName("header") D. document.getElementById("header") 4.以下哪种 CSS 选择器的优先级最高? A. 类选择器 (.class) B. 标签选择器 (p) C. ID 选择器 (#id) D. 内联样式 (style="...") 5.在 JavaScript 中,let 和 const 的共同点是? A. 声明的变量都可以被重新赋值 B. 都有块级作用域 C. 声明的变量都可以被重新声明 D. 都没有变量提升 第二部分:填空题 1.在 HTML 中,用于创建超链接的标签是 <______>。 2.要使一个 div 元素在 Flex 布局中水平居中,需要为其父元素设置 CSS:display: flex; justify-content: ______;。 3.在 JavaScript 中,______ 语句用于声明一个常量。 4.CSS 中,margin: 10px 20px; 表示上下边距为 ______px,左右边距为 20px。 5.通过 document.______('button') 可以获取文档中所有的 <button> 元素 第三部分:综合编程题 定义一个form表单,要求form表单中有两个文本输入框,可以输入数字,然后再准备一个button 点击这个按钮可以将上述两个文本框中的数据做一个加法运算 再将结果显示在结果框内(新建一个文本输入框 框中的value值是通过计算获得) 定义一个form表单 有用户名 邮箱 手机号 家庭住址等信息,要求使用正则表达式给出相应的输入要求,并做出对应的提示.
最新发布
09-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值