JavaScript学习系列2 DOM
DOM = document + object+model
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping List</title>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping List</title>
<!-- 样式表 -->
<style>
p {
color: yellow;
font-family: "arial", sans-serif;
font-size:1.2em;
}
body {
color:white;
background-color:black;
}
.special {
font-style:italic;
}
h2.special {
text-transform:uppercase;
}
#purchases {
border:1px solid white;
background-color:#333;
color:#ccc;
padding:1em;
}
#purchases li {
font-weight:bold;
}
</style>
</head>
<body>
<h1>what to buy</h1>
<h2 class="special">This is uppercase</h2>
<p title="a gentle reminder">Don't forget to buy this stuff.</p>
<ul id="purchases">
<li>A tin of beans</li>
<li class="special">Cheese</li>
<li >Milk</li>
</ul>
<!-- Mark-up goes here -->
<script src="file.js"></script>
</body>
</html>
<style>
p {
color: yellow;
font-family: "arial", sans-serif;
font-size:1.2em;
}
body {
color:white;
background-color:black;
}
.special {
font-style:italic;
}
h2.special {
text-transform:uppercase;
}
#purchases {
border:1px solid white;
background-color:#333;
color:#ccc;
padding:1em;
}
#purchases li {
font-weight:bold;
}
</style>
</head>
<body>
<h1>what to buy</h1>
<h2 class="special">This is uppercase</h2>
<p title="a gentle reminder">Don't forget to buy this stuff.</p>
<ul id="purchases">
<li>A tin of beans</li>
<li class="special">Cheese</li>
<li >Milk</li>
</ul>
<!-- Mark-up goes here -->
<script src="file.js"></script>
</body>
</html>
JavaScript
<!-- alert(typeof document.getElementById("purchases"));
<!-- alert(document.getElementsByTagName("*").length);
alert(document.getElementsByClassName("special").length);
var shopping = document.getElementById("purchases");
alert(shopping.getElementsByTagName("*").length);
function add(num1,num2) {
var total = num1*num2;
alert(3 == "3");
}
<!-- alert(document.getElementsByTagName("*").length);
alert(document.getElementsByClassName("special").length);
var shopping = document.getElementById("purchases");
alert(shopping.getElementsByTagName("*").length);
function add(num1,num2) {
var total = num1*num2;
alert(3 == "3");
}
HTML 5 DOM中新增了一个令人期待已久的方法:getElementsByClassName。这方法让我们能够Class属性中的类名来访问元素。但是这个方法比较新,只有较新的浏览器才支持他。
因此我们可以 写一个方法来实现这个功能。
function getElementByClassName(node, classname) {
if(node.getElementsByClassName) {
return node.getElementsByClassName(classname);
} else {
var results = new Array();
var elems = node.getElementsByTagName("*");
for(var i=0; i<elems.length; i++) {
if(elems[i].className.indexOf(classname) != -1) {
results[results.length] = elems[i];
}
}
return results;
}
}
if(node.getElementsByClassName) {
return node.getElementsByClassName(classname);
} else {
var results = new Array();
var elems = node.getElementsByTagName("*");
for(var i=0; i<elems.length; i++) {
if(elems[i].className.indexOf(classname) != -1) {
results[results.length] = elems[i];
}
}
return results;
}
}
var shopping = document.getElementById("purchases");
alert(shopping.getAttribute("title"));
shopping.setAttribute("title", "a list of goods");
alert(shopping.getAttribute("title"));
alert(shopping.getAttribute("title"));
shopping.setAttribute("title", "a list of goods");
alert(shopping.getAttribute("title"));
setAttribute:先创建属性,然后设置他的值
这里有个非常值得关注的细节:通过setAttribute对文档做出修改后,在通过浏览器view source选项去查看文档的源代码时看到的仍将是改变前的属性值,
也就是说setAttribute做出的修改不会反映在文档本身的源代码里。这种”表里不一“的现象源自DOM的工作模式:先加载文档的静态内容,再动态刷新,动态刷新
不会影响文档的静态内容。这正是DOM的真正威力:对页面内容进行刷新却不需要再浏览器里刷新页面。