JS和CSS的初步入门(JS可以取得所有p的内容并显示)

时间:2021-10-05 08:39:41
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Shopping list</title>
<style type="text/css">
p { /* 对所有p进行说明 */
color: yellow;
font-family: "arial", sans-serif;
font-size: 1.2em;
}
body {
color: white; /* 没有专门说明的话,body的所有文字都是白色 */
background-color: black; /* 全屏黑色背景 */
}
#purchases { /* 对某一个id进行说明 */
border: 1px solid white; /* 定制边框 */
background-color: #333;
color: #ccc;
padding: 1em;
}
#purchases li { /* 对某一个id的下属进行说明 */
font-weight: bold;
} </style>
</head>
<body>
<h1>What to buy</h1>
<h1>What to buy</h1>
<p title1="a gentle reminder">Don't forget to buy this stuff.</p>
<p>This is just a test</p>
<ul id="purchases">
<li>A tin of beans</li>
<li>Cheese</li>
<li>Milk</li>
</ul>
<script type="text/JavaScript">
var paras = document.getElementsByTagName("p");
for (var i=0; i< paras.length; i++) {
var title_text = paras[i].getAttribute("title1");
if (title_text != null) {
alert(title_text);
}
}
</script>
</body>
</html>