使用案例
表单验证:
<html>
<head>
<script type="text/javascript">
function check(username){
if(username.length<=3){
document.getElementById("usernameError").innerHTML=
"<font color='red'>用户名长度需大于3</font>";
} else{
document.getElementById("usernameError").innerHTML="";
}
}
</script>
</head>
<body>
用户名:<input type="text" name="uName" onblur="check(this.value);"/>
<span id="usernameError"></span>
</body>
</html>
onblur代表元素失去焦点的时候触发的事件。
this关键字代表此标签,此时指input标签。this.value指input标签的value值。
document.getElementById(""); 按id获得元素。
innerHTML:设置两个标签之间的内容,这个内容是动态指定的。可以是<div>标签或<span>标签。
-------------------------------------------------------------------------------------------------------------
输出浏览器信息:
<html>
<body>
<script type="text/javascript">
window.document.write("浏览器:"+window.navigator.appName)
document.write("<br>");
document.write("浏览器版本:"+parseFloat(navigator.appVersion))
</script>
</body>
</html>
window对象是浏览器窗口对象,是JavaScript中的根对象。window可以省略不写。
document是window对象的属性,而本身也是一个对象。是文档对象。
navigator是window对象的属性,而本身也是一个对象。浏览器对象。
-------------------------------------------------------------------------------------------------------------
<script type="text/javascript">
var i = 0;
function addPoint(){
var html=$("#selectDirection").html();
$("#pointTable").append("<tr id="+i+"><td><input type='text' style='width: 100'/></td><td><select>"
+html+
"</select></td><td><img src='/images/nolines_plus.gif' onclick='javascript:addPoint();'/></td><td>"+
"<img src='/images/nolines_minus.gif' onclick='javascript:removePoint("+i+");'/></td></tr>");
i = i + 1;
}
function removePoint(id){
$("#"+id+"").remove();
}
</script>