標籤:setattr ref 標籤 out context load before function type
1、history屬性<body>
<h1>第一個介面</h1>
<a href="js02history.html">當前頁面</a>
<a href="js03history.html">下一個頁面</a>
<a href="javascript:history.forward()">forward()前進一個介面</a>
<a href="javascript:history.go(1)">go(1)前進一個介面</a>
</body>
<body>
<h1>第二個介面</h1>
<a href="javascript:history.back()">back()後退一個介面</a>
<a href="javascript:history.go(-1)">go(-1)後退一個介面</a>
</body>2、location屬性 <script type="text/javascript">
document.write("host值為:"+location.host+"<br/>")
document.write("hostname值為:"+location.hostname+"<br/>")
document.write("href值為:"+location.href+"<br/>")
document.write("hash值為:"+location.hash+"<br/>")
document.write("search值為:"+location.search+"<br/>")
</script>
</head>
<body>
<input type="text">
<input type="button" value="重新整理當前頁面" onclick="location.reload()">
<input type="button" value="替換當前頁面" onclick="location.replace(‘http://www.bdqn.cn‘)">
</body>3、document屬性 <style type="text/css">
body{font-size:14px;
line-height:30px;
}
input{margin:1px;
width:90px;
font-size:12px;
padding:0;
}
#node{
width:100px;
font-size:24px;
font-weight:bold;
float: left;
}
</style>
<script type="text/javascript">
/*改變層內容*/
function changeLink(){
document.getElementById("node").innerHTML="<h1>改變</h1>";
//document.getElementById("node").innerText="<h1>改變</h1>";
}
/*擷取所有input標籤中所有的value*/
function all_input(){
var allInput= document.getElementsByTagName("input");
/*聲明變數 接收所有input標籤中所有的value*/
var str="";
for(var i=0;i<allInput.length;i++){
str+=allInput[i].value+"<br/>";
}
/*把str擷取的值 給 p標籤*/
document.getElementById("s").innerHTML=str;
}
/*擷取所有name屬性值是season的value*/
function s_input(){
var season= document.getElementsByName("season");
/*聲明變數 接收所有input標籤中所有的value*/
var str="";
for(var i=0;i<season.length;i++){
str+=season[i].value+"<br/>";
}
/*把str擷取的值 給 p標籤*/
document.getElementById("s").innerHTML=str;
}
</script>
</head>
<body>
<div id="node">新浪</div>
<input name="b1" type="button" value="改變層內容" onclick="changeLink();" /><br />
<br /><input name="season" type="text" value="春" />
<input name="season" type="text" value="夏" />
<input name="season" type="text" value="秋" />
<input name="season" type="text" value="冬" />
<br /><input name="b2" type="button" value="顯示input內容" onclick="all_input()" />
<input name="b3" type="button" value="顯示season內容" onclick="s_input()" />
<p id="s"></p>
</body>4、open<script type="text/javascript">
function openNew(){
window.open(
"http://www.baidu.com",
"百度頁面",
"height=400,width=400"
);
}
</script>
</head>
<body>
<input type="button" value="開啟新的視窗" onclick="openNew()">
</body>5、定時函數<script type="text/javascript">
var time=0;
function count(){ //計數的方法
document.getElementById("context").innerHTML="時間:"+(++time); }
var interval,timeout;
//定時函數
function setI(){ //setInterval函數 周期執行
interval=setInterval("count()",1000);
}
function setT(){ //setTimeout函數 執行一次
timeout= setTimeout("count()",1000);
}
//清除定時函數
function clearI(){//清除setInterval函數
clearInterval(interval);
}
function clearT(){//清除setTimeout函數
clearTimeout(timeout);
} </script>
</head>
<body>
<div id="context"></div>
<input type="button" value="setInterval函數" onclick="setI()">
<input type="button" value="setTimeout函數" onclick="setT()"><br/>
<input type="button" value="清除setInterval函數" onclick="clearI()">
<input type="button" value="清除setTimeout函數" onclick="clearT()">
</body>6、訪問節點<script type="text/javascript">
/*
* nodeName:
* 元素節點顯示的是標籤名稱
* 屬性節點顯示的是屬性名稱
* 文本節點顯示的是 #text
* 文檔節點顯示的是#document
* nodeValue;
* 文本節點顯示的是文本
* 屬性節點顯示的是屬性值
*
* nodeType:
* 1 元素節點
* 2 屬性節點
* 3 文本節點
* 8 注釋
* 9 文檔
* */
window.onload=function(){
var ul= document.getElementsByTagName("ul")[0];
/* alert("節點名稱:"+ul.nodeName);
alert("節點類型:"+ul.nodeType);*/
/*擷取ul中的第一個li*/
var li=ul.firstElementChild;
alert("節點名稱:"+li.nodeName);
alert("節點類型:"+li.nodeType);
alert("節點內容:"+li.childNodes[0].nodeValue); /*改變小貓咪圖片的寬度*/
var image=document.getElementsByName("cat")[0];
image.setAttribute("width","200px");
//擷取src的值
alert(image.getAttribute("src"));
} </script>
</head>
<body>
<ul>
<li>小強1</li>
<li>小強2</li>
<li>小強3</li>
</ul><img src="images/cat.jpg" name="cat">
</body>7、節點的增刪改<script type="text/javascript"> window.onload=function(){
var ul= document.getElementsByTagName("ul")[0];
/*新增節點*/
var newLi= document.createElement("li");
newLi.innerHTML="小黑";
ul.appendChild(newLi);
/*擷取ul第三個li*/
var second= ul.getElementsByTagName("li")[2];
ul.insertBefore(newLi,second);
/*clone*/
var ul2= document.getElementsByTagName("ul")[0].cloneNode(true);
document.getElementById("d").appendChild(ul2);
/*刪除節點*/
var reNode= ul.getElementsByTagName("li")[0];
// ul.removeChild(reNode);
/*替換節點*/
ul.replaceChild(newLi,reNode);
} </script>
</head>
<body>
<ul>
<li>小強1</li>
<li>小強2</li>
<li>小強3</li>
</ul>
<div id="d">
</div>
</body>
javascript(一)