javascript常見用法,比如,編碼解碼、間隔及掩飾執(zhí)行、檢查輸入是否為數(shù)字、獲取、創(chuàng)建和刪除節(jié)點等等
js解碼和編碼.html
代碼如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript的編碼和解碼</title>
<script type="text/javascript">
function gel(id) {
return document.getElementById(id);
}
window.onload = function() {
//alert(document.getElementById("span1").innerHTML
gel("btn1").onclick = function() {
alert(encodeURI(gel("span1").innerHTML));
};
gel("btn2").onclick = function() {
alert(decodeURI(gel("span1").innerHTML));
};
};
</script>
</head>
<body>
<span id="span1">瘋漢三雄起了!</span>
<input type="button" id="btn1" value="編碼后" />
<input type="button" id="btn2" value="解碼后" />
</body>
</html>
js中setInterval和setTimeout的使用.html
代碼如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>js中setInterval和setTimeout的使用</title>
<script type="text/javascript">
var time = 10;
var id = 0;
function gel(id) {
return document.getElementById(id);
}
function dectime() {
if (time > 0) {
time--;
gel("timespan").innerHTML = time;
} else {
//清除時針
clearInterval(id);
}
}
window.onload = function() {
id = setInterval(dectime, 1000);
};
</script>
</head>
<body>
<span >倒計時<span id="timespan" style="color: red;"></span>秒</span>
</body>
</html>
js檢查輸入是否為數(shù)字.html
代碼如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>js檢查輸入是否為數(shù)字</title>
<script type="text/javascript">
window.onload= function() {
document.getElementById("btn1").onclick = function() {
var i = prompt("輸入要判斷的值");
//window.alert(i);
if (!isNaN(i)) {
window.alert("是數(shù)字");
} else {
window.alert("不是數(shù)字");
}
};
}
</script>
</head>
<body>
<input type="button" id="btn1" value="判斷數(shù)字" />
</body>
</html>
js動態(tài)獲取、創(chuàng)建和刪除節(jié)點.html
代碼如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js動態(tài)獲取、創(chuàng)建和刪除節(jié)點</title>
<script type="text/javascript">
function gel(id) { return document.getElementById(id); }
window.onload = function () {
gel("btnProAdd").onclick = function () {
//在proList下面增加子節(jié)點
var linew = document.createElement("li");
linew.innerHTML = prompt("輸入要新增的省份");
gel("proList").appendChild(linew);
//重新綁定所有的點擊刪除事件
DelLiOnClick();
};
//雙擊li子節(jié)點,刪除它
function DelLiOnClick() {
//1.首先得到所有的子節(jié)點
var liNodes = gel("proList").childNodes;
for (var i = 0; i < liNodes.length; i++) {
liNodes[i].onclick = function () {
//alert(liNodes[i]).innerHTML;//因為onclick綁定的是匿名函數(shù),所以i到這里永遠(yuǎn)只會是7
//下面是正確的刪除方法, 使用this.因為觸發(fā)onclick事件的永遠(yuǎn)是你選中的li
this.parentNode.removeChild(this);
};
}
}
};
</script>
</head>
<body>
<ul id="proList">
<li>山西</li>
<li>河南</li>
<li>北京</li>
</ul>
<input type="button" value="新增省份" id="btnProAdd" />
</body>
</html>
js中setInterval和setTimeout的使用.html
代碼如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>js中setInterval和setTimeout的使用</title>
<script type="text/javascript">
var time = 10;
var id = 0;
function gel(id) {
return document.getElementById(id);
}
function dectime() {
if (time > 0) {
time--;
gel("timespan").innerHTML = time;
} else {
//清除時針
clearInterval(id);
}
}
window.onload = function() {
id = setInterval(dectime, 1000);
};
</script>
</head>
<body>
<span >倒計時<span id="timespan" style="color: red;"></span>秒</span>
</body>
</html>
js動態(tài)添加表格數(shù)據(jù).html
代碼如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>動態(tài)添加表格數(shù)據(jù)</title>
<script type="text/javascript">
var mailArr = [
{ "title": "一個c#問題", "name": "張三", "date": "2014-03-21" },
{ "title": "一個javascript問題", "name": "李四", "date": "2014-03-21" },
{ "title": "一個c問題", "name": "五五", "date": "2014-03-21" },
{ "title": "一個c++問題", "name": "趙六", "date": "2014-03-21" }
];
window.onload = function () {
var tab = document.getElementById("tb");
//把mailArr循環(huán)遍歷方式以tr的方式加入表格中
for (var rowindex = 0; rowindex < mailArr.length; rowindex++) {
var tr = document.createElement("tr");
var th1 = document.createElement("th");
var th2 = document.createElement("th");
var th3 = document.createElement("th");
var th4 = document.createElement("th");
th1.innerHTML = "<input type='checkbox'/>";
th2.innerHTML = mailArr[rowindex].title;
th3.innerHTML = mailArr[rowindex].name;
th4.innerHTML = mailArr[rowindex].date;
tr.appendChild(th1);
tr.appendChild(th2);
tr.appendChild(th3);
tr.appendChild(th4);
tab.appendChild(tr);
}
};
</script>
</head>
<body>
<table id="tb" border="1px;" style="border-collapse: collapse;">
<tr>
<th>序列</th>
<th>標(biāo)題</th>
<th>發(fā)郵人</th>
<th>發(fā)件時間</th>
</tr>
<!-- 循環(huán)增加 -->
</table>
</body>
</html>
更多建議: