JavaScript第一章
ch01
<!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>
<title></title>
<script type="text/javascript" src="Scripts/common.js"></script>
</head>
<body>
<div id="divFirst">
</div>
<div id="divSecond">
</div>
<div id="divThird">
</div>
<script type="text/javascript">
$('divFirst').innerHTML = "<a href='http://www.baidu.com'>Go to baidu</a>";
updateHtml('divSecond','<input type="text" value="我是文本框"/>');
updateText('divThird','<pre>文本内容,不是HTML</pre>');
writeHtml('同学,你好!');
writeP("我是段落内容");
writeTable(10, 3);
writeHtml(getDateString());
writeHtml("<br>");
writeSelectList(['bmw', 'benz', 'audi']);
writeList(['apple', 'banana', 'cat']);
</script>
<ul>
<li onmouseover="setColor(this,'red')" onmouseout="setColor(this,'white')">apple</li>
<li onmouseover="setColor(this,'red')" onmouseout="setColor(this,'white')">banana</li>
<li onmouseover="setColor(this,'red')" onmouseout="setColor(this,'white')">cat</li>
</ul>
</body>
</html>
Ch02
//获取dom元素var $ = function (id) {
return document.getElementById(id);
}
//输出html
var writeHtml = function (str) {
document.write(str);
}
//更新元素HTML内容
var updateHtml = function(id,html){
$(id).innerHTML = html;
}
var updateText = function(id,text){
$(id).innerText = text;
}
//输出段落
var writeP = function (content) {
writeHtml("<p>" + content + "</p>");
}
//输出表格(行,列)
var writeTable = function (row, col) {
writeHtml("<table border=1>")
for (var i = 0; i < row; i++) {
writeHtml("<tr>");
for (var j = 0; j < col; j++) {
writeHtml("<td>" + (i + 1).toString()
+ "行" + (j + 1).toString() + "列</td>")
}
writeHtml("</tr>");
}
writeHtml("</table>");
}
//获取当前系统时间
var getDateString = function () {
return new Date().toLocaleString();
}
//改变背景颜色
var setColor = function (o, color) {
o.style.backgroundColor = color;
}
//输出列表
var writeList = function (arr) {
writeHtml("<ul>");
for (var i = 0; i < arr.length; i++) {
writeHtml('<li>' + arr[i] + '</li>');
}
writeHtml("</ul");
}
//输出下拉框
var writeSelectList = function (arr) {
writeHtml("<select>");
for (var i = 0; i < arr.length; i++) {
writeHtml('<option>' + arr[i] + '</option>')
}
writeHtml("</select>");
}