HTML中的一些对象()
使用DOM-Document Object Model操作对象getElementById();getElementsByName();等
而改变标签里的内容要用.innerHTML,改变链接用.href例如:
<!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">
function changerButton(){
var link = document.getElementById("links");
link.innerHTML = "NetEasy";//改变a标签里的内容即改变了Sina
link.href = "http://www.163.com";//链接改变了
}
</script>
</head>
<body>
<a href="http://www.sina.com">Sina</a>
<input type="button" value="Change" id="myButton" οnclick="changerButton()"/>
</body>
</html>
javascript里的一些应用:例如
<!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">
var count = 0;
var st;
function showDiv() {
count++;
if(count >=10) {
clearTimeout(st);
} else {
var div = document.getElementById("myDiv");
div.innerHTML = div.innerHTML + "I love you!";
st = setTimeout("showDiv()",200);
}
}
</script>
</head>
<body>
<div id="myDiv"></div>
<input type="button" value="I love you!" οnclick="showDiv()" />
</body>
</html>
script里可以嵌套if语句等
一些网页里的时间显示也可以用javascript来写例如显示时间的走动的简单的页面:
<!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>
</head>
<body>
<div id="timer"></div>
<input type="button" value="showTime" οnclick="showTimer()" />
<script type="text/javascript">
function showTimer() {
var mydate = new Date();
var text = mydate.toLocaleString();
var div = document.getElementById("timer");
div.innerHTML = text;
setTimeout("showTimer()",1000);
}
window.onload = showTimer();
</script>
</body>
</html>
![](https://i-blog.csdnimg.cn/blog_migrate/df7e4dd8be05d5cf317131a56461b7e0.png)
图中时间显示为走动的状态(时间在变)。