Fork me on GitHub
Javascript的this例子

<!--这个程序使用了javascript中的this,this可以减少很多不必要的代码量,并且提高代码的效率-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script language="javascript">
function processData(form){
for (var i = 0; i < form.Beatles.length; i++) {
if (form.Beatles[i].checked) {
break;
}
}
var beatle = form.Beatles[i].value;
var song = form.song.value;
alert("Checking whether"+"   "+ song+"   "+ "features"+"   "+ beatle+"   "+ "...");
}

function verifySong(entry){//这里的entry是随便起的也可以叫别的名
var song = entry.value;
alert("Checking whether" +"   "+ song+"   " + "is a Beatles tune...");
}
</script>
</head>
<body>
<form onSubmit="return false">
Choose your favorite Beatle:
<input type="radio" name="Beatles" value="John Lennon" checked/>John  <!--看到这里的checked了吧可以这样太简单了-->
<input type="radio" name="Beatles" value="Paul McCartney"/>Paul
<input type="radio" name="Beatles" value="George Harrison"/>George
<input type="radio" name="Beatles" value="Ringo Starr"/>Ringo
<p>
Enter the name of your favorite Beatles song:
<br>
<input type="text" name="song" value="Eleanor Rigby" onChange="verifySong(this)"/>
<p>
<input type="button" name="process" value="Process Request..." onClick="processData(this.form)"/>
<!--因为这个按钮是要提交表单的,直接将form传到指定的函数中-->
</form>
</body>
</html>

posted on 2010-07-12 14:21  HackerVirus  阅读(124)  评论(0编辑  收藏  举报