<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>myTest</title>
<style>
body, ul{
margin: 0;
padding: 0;
}
body {
color: white;
background-color: black;
}
#purchases {
border: 1px solid white;
background-color: #333;
color: #ccc;
padding: 1em;
}
#purchases li {
font-weight: bold;
}
p {
color: yellow;
font-family: "arial", sans-serif;
font-size: 1.2em;
}
</style>
</head>
<body>
<h1>What to buy ?</h1>
<p title="a gentle reminder">Don't foger to buy this stuff.</p>
<ul id="purchases">
<li>shoes</li>
<li class="sale">hat</li>
<li class="sale important">towel</li>
</ul>
<script>
var mood = "about 5'10\" tall", age = 32;
alert(mood);
alert(age);
1、getElementByID
alert(typeof document.getElementById('purchases'));
2、getElementByTagName
alert(document.getElementsByTagName('li').length);
alert(document.getElementsByTagName('*').length);
var shopping = document.getElementById('purchases');
var items = shopping.getElementsByTagName('*');
alert(items.length);
3、getElementsByClassName
4、getAttribute
var paras = document.getElementsByTagName('p');
for (var i = 0; i < paras.length; i++) {
var titleText = paras[i].getAttribute('title');
if (titleText != null) alert(titleText);
}
var shopping = document.getElementById("purchases");
alert(shopping.getAttribute("title"));
shopping.setAttribute("title", "a list of goods");
alert(shopping.getAttribute("title"));
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Image Gallery</title>
<style>
body {
font-family: "Helvetica", "Arial", serif;
color: #333;
background-color: #ccc;
margin: 1em 10%;
}
h1 {
color: #333;
background-color: transparent;
}
a {
color: #c60;
background-color: transparent;
font-weight: bold;
text-decoration: none;
}
ul {
padding: 0;
}
li {
float: left;
padding: 1em;
list-style: none;
}
img {
display: block;
clear: both;
width: 50%;
}
</style>
</head>
<body>
<h1>My js---Gallery</h1>
<ul>
<li>
<a href="C:\Users\zwh\Desktop\simple\images\1.jpg"
onclick="showPicture(this); return false;"
title="A beautiful alien globe">外星球</a>
</li>
<li>
<a href="C:\Users\zwh\Desktop\simple\images\2.jpg"
onclick="showPicture(this); return false;"
title="A beautiful girl">美女</a>
</li>
<li>
<a href="C:\Users\zwh\Desktop\simple\images\3.jpg"
onclick="showPicture(this); return false;"
title="the many stars">星星</a>
</li>
<li>
<a href="C:\Users\zwh\Desktop\simple\images\4.jpg"
onclick="showPicture(this); return false;"
title="The beautiful view">风景</a>
</li>
</ul>
<img id="placeholder" src="C:\Users\zwh\Desktop\simple\images\1.jpg" alt="my image gallery">
<p id="description">Choose an image.</p>
<script>
function showPicture (whichPicture) {
var sourceHref = whichPicture.getAttribute("href");
var soreceTitle = whichPicture.getAttribute("title");
var placeholder = document.getElementById("placeholder");
var description = document.getElementById("description");
description.firstChild.nodeValue = soreceTitle;
placeholder.setAttribute("src", sourceHref);
}
function countBodyChildren() {
var bodyElement = document.getElementsByTagName("body")[0];
alert(bodyElement.nodeType);
}
</script>
</body>
</html>