您的位置:首页 > Web前端 > JavaScript

关于js的练习集锦

2009-12-02 10:48 232 查看
练习一:



<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>基本html <form>标签的使用</title>
<script language="javascript" type="text/jscript">
function f()
{
document.getElementById("output").innerHTML += "用户信息如下:<br>";
document.getElementById("output").innerText += document.getElementById("name").value;
document.getElementById("output").innerText += document.getElementById("password").value;
var obj = document.getElementsByName("radio");
for(var i = 0; i < obj.length; i++)
{
if(obj[i].checked)
{
document.getElementById("output1").innerText=obj[i].value;
}
}
var obj = document.getElementsByName("checkbox");
for(var i = 0; i < obj.length; i++)
{
if(obj[i].checked)
{
document.getElementById("output2").innerText=obj[i].value;
}
}
output3.src=document.getElementById("photo").value;
document.getElementById("output4").innerText=document.getElementById("textfield").value;
}
</script>
</head>

<body>
<form method="post">
<fieldset>
<legend style="color:#F93">用户名和密码:</legend>
用户名:<input type="text" name="name" />
密码:<input type="password" name="password"/>
</fieldset>
<fieldset>
<legend style="color:#F93">性别:</legend>
性别:<input type="radio" name="radio" id="radio" value="男"/>男 <input type="radio" name="radio" id="radio" value="女"/>女 <input type="radio" name="radio" id="radio" value="保密"/>保密
</fieldset>
<fieldset>
<legend style="color:#F93">爱好:</legend>
爱好:<input type="checkbox" name="checkbox" value="计算机"/> 计算机 <input type="checkbox" name="checkbox" value="旅游"/>旅游 <input type="checkbox" name="checkbox" value="购物"/>购物
</fieldset>
<fieldset>
<legend style="color:#F93">照片:</legend>
个性照片上传:<input type="file" name="photo" id="fileField" />
</fieldset>
<fieldset>
<legend style="color:#F93">自我评价:</legend>
<textarea name="textfield" id="textfield" cols="45" ></textarea>
</fieldset>
<fieldset>
<legend style="color:#F93">提交:</legend>
<input type="button" name="button" id="button" value="提交" onclick="f()" /> <input type="reset" name="button" id="button" value="重置"/>
</fieldset>
</form>
<pre>
<span id="output" ></span>
<span id="output1"></span>
<span id="output2"></span>
<img id="output3"></img>
<span id="output4"></span>
</pre>

</body>
</html>

练习二:



js文件

// JavaScript Document
function f(){
var sum;
var info=document.getElementById("textfield").value;
var obj = document.getElementsByName("radio");
var v;
for(var i = 0; i < obj.length; i++)
{
if(obj[i].checked)
{
v=obj[i].value;
break;
}
}
var obj = document.getElementsByName("checkbox");
var va =0;
for(var i = 0; i < obj.length; i++)
{
if(obj[i].checked)
{
va +=parseInt(obj[i].value);
}
}
sum=info*v+va;
document.getElementById("textfield2").value= sum;

}

// JavaScript Document
function f(){
var sum;
var info=document.getElementById("textfield").value;
var obj = document.getElementsByName("radio");
var v;
for(var i = 0; i < obj.length; i++)
{
if(obj[i].checked)
{
v=obj[i].value;
break;
}
}
var obj = document.getElementsByName("checkbox");
var va =0;
for(var i = 0; i < obj.length; i++)
{
if(obj[i].checked)
{
va +=parseInt(obj[i].value);
}
}
sum=info*v+va;
document.getElementById("textfield2").value= sum;

}

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="2.js" language="javascript" type="text/jscript">
</script>
</head>

<body>
<p>家用电脑订购单:</p>
<hr color="#0033FF"/>
<form>
<table width="822" height="56" border="0">
<tr>
<td colspan="4">家用电脑:</td>
</tr>
<tr>
<td width="70"><div align="right">数量:</div></td>
<td width="114">
<label>
<input name="textfield" type="text" id="textfield" size="8" />
</label>

</td>
<td width="54">单价:</td>
<td width="566">
<label>
<input type="radio" name="radio" id="radio1" value="5000" />
</label>
5000元
<label>
<input type="radio" name="radio" id="radio2" value="7000" />
</label>
7000元
</td>
</tr>
</table>
<table width="822" border="0">
<tr>
<td colspan="4"> 加强配置选项:</td>
</tr>
<tr>
<td width="198">
<label>
<input type="checkbox" name="checkbox" id="checkbox1" value="300"/>
</label>
64M内存 300


</td>
<td width="186">
<label>
<input type="checkbox" name="checkbox" id="checkbox2" value="400"/>
</label>
56K Moden 400元

</td>
<td width="199">
<label>
<input type="checkbox" name="checkbox" id="checkbox3" value="800"/>
</label>
彩喷 800元

</td>
<td width="221">
<label>
<input type="checkbox" name="checkbox" id="checkbox4" value="900"/>
</label>
摄像头 900元
</td>
</tr>
</table>
<hr color="#00FF33"/>

<label></label>
<table width="822" border="0">
<tr>
<td width="87">
<label>
<input type="button" name="button" id="button" value="计算货款" onclick="f()"/>
</label>

</td>
<td width="103"><div align="center">总计货款:</div></td>
<td width="618">
<label>¥
<input type="text" name="textfield2" id="textfield2" />
</label>

</td>
</tr>
</table>
<label></label>
<label></label>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</form>
</body>
</html>

练习三:



js文件

// JavaScript Document
//设置年
function YearSet(){
var birYear = document.myForm.birYear;
if(birYear.options.length == 1){
for(i = new Date().getYear(); i > 1960; i--){
birYear.add(new Option(i,i));
}
}
}
//设置月
function MonthSet(){
var birMonth = document.myForm.birMonth;
if(birMonth.options.length == 1){
for(j = 12; j > 0; j--){
birMonth.add(new Option(j, j));
}
}
}
//设置日期
function DaySet(){
var birYear = document.myForm.birYear;
var birMonth = document.myForm.birMonth;
var birDay = document.myForm.birDay;
/*if(birDay.options.length != 1){
var num = birDay.options.length;
for(i = 1; i < num; i++){
birDay.remove(i);
}
}*/
var count = 31;
birDay.options.length = 0;
if(birMonth.value != null){
var month = birMonth.value;
if(birYear.value != null && month == 2 ){
count = 28 + isLeapYear(birYear.value);
}
else if(month == 4 || month == 6 || month == 9 || month == 11){
count = 30;
}
}
for(m = count; m > 0; m--){
birDay.add(new Option(m, m),birDay.options.length+1);
}
}
//是否是闰年
function isLeapYear(year){
return year%4 == 0 && year%100 != 0 ||year % 400 == 0;
}
//检查Email
function EmailOnFocus(){
if(myForm.email.value == "" || myForm.email.value == null)
{
document.getElementById("EmailPromptInfo").innerHTML="请输入正确的邮箱,完成注册";
}
}

function CheckEmail(email){
if(email != null && email != "")
{
if(email.search("^(?://w+//.?)*//w+@(?://w+//.?)*//w+$")){
document.getElementById("EmailPromptInfo").innerHTML = "<font color='red'>请输入正确的电子邮件!</font>";
}
else{
document.getElementById("EmailPromptInfo").innerHTML = "<font color='green'>正确!</font>";
}
}
else{
document.getElementById("EmailPromptInfo").innerText = "";
}
}

//检查密码
function PwdOnFocus(){
if(myForm.password.value == "" || myForm.password.value == null)
{
document.getElementById("PwdPromptInfo").innerHTML="密码必须由6-20个字符组成";
}
}

function CheckPassword(password){
if(password != null && password != "")
{
if(password.length < 6 || password.length > 20){
document.getElementById("PwdPromptInfo").innerHTML="<font color='red'>密码必须在6-20位之间.</font>";
}
else if(password.search("[/da-zA-Z]*/d+[a-zA-Z]+[/da-zA-Z]*")){
document.getElementById("PwdPromptInfo").innerHTML="<font color='red'>密码过于简单,请修改.</font>";
}
else{
document.getElementById("PwdPromptInfo").innerHTML="<font color='green'>正确!</font>";
}
}
else{
document.getElementById("PwdPromptInfo").innerText = "";
}
}

//检查姓名
function NameOnFocus(){
if(myForm.name.value == "" || myForm.name.value == null)
{
document.getElementById("NamePromptInfo").innerHTML = "请输入<font color='red'><b>真实中文姓名</b></font>,方便朋友查找";
}
}

function CheckName(name){
if(name != null && name != "")
{
if(name.search("^[//u4E00-//u9FA5//uF900-//uFA2D]+$")){
document.getElementById("NamePromptInfo").innerHTML="请输入<font color='red'><b>真实中文姓名</b></font>";
}
else{
document.getElementById("NamePromptInfo").innerHTML="<font color='green'>正确!</font>";
}
}
else{
document.getElementById("NamePromptInfo").innerText = "";
}
}

//提交进行最后检查
function CheckAll(){
var epi = document.getElementById("EmailPromptInfo").innerText;
var ppi = document.getElementById("PwdPromptInfo").innerText;
var npi = document.getElementById("NamePromptInfo").innerText;
var sex = document.getElementsByName("sex");
var count = false;
for(i = 0; i < sex.length; i++){
if(sex[i].checked){
count = true;
}
}
var birYear = document.getElementById("birYear").value;
var birMonth = document.getElementById("birMonth").value;
var birDay = document.getElementById("birDay").value;
if(epi == "正确!" && ppi == "正确!" && npi == "正确!" && count && birYear != "请选择" && birMonth != "--" && birDay != "--"){
alert("恭喜,已完成注册!");
}
else{
alert("信息填写不完整,请认真检查!");
}

}

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="3.js" language="javascript" type="text/jscript">
</script>
<style type="text/css">
<!--
.STYLE1 {color: #0033CC}
-->
</style>
</head>

<body>
<form name="myForm">

<table width="997" border="0">
<tr>
<td><div align="right">已经有人人网帐号?<span class="STYLE1">请登陆</span></div></td>
</tr>
<tr>
<td>加入人人网,找回老朋友,结交新朋友</td>
</tr>
</table>
<p>
<label for="emil">电子邮件:</label>
<input type="text" name="text" id="emil" cols="24" onFocus="EmailOnFocus()" onBlur="CheckEmail(this.value)"/>
<span id="EmailPromptInfo" class="promptInfo"></span>
</p>
<p>
<span class="spanStyle">如果没有邮箱,你可以用<font color="#99CCCC">帐号</font>注册</span>
</p>
<p>
<label for="pass">设置密码:</label>
<input type="password" name="pass" id="pass" cols="24" onFocus="PwdOnFocus()" onBlur="CheckPassword(this.value)"/>
<span id="PwdPromptInfo" class="promptInfo"></span>
</p>
<p>
<label for="name">真实姓名:</label>
<input type="text" name="name" id="name" cols="24" onFocus="NameOnFocus()" onBlur="CheckName(this.value)"/>
<span id="NamePromptInfo" class="promptInfo"></span>
</p>
<p>
<label for="sex">性别:</label>
<input type="radio" name="sex" id="sex" value="男"/>男 <input type="radio" name="sex" id="sex" value="女"/>女
</p>
<p>
生日:<select id="birYear" onFocus="YearSet()">
<option value="请选择"/>请选择
</select>年
<select id="birMonth" onFocus="MonthSet()">
<option value="--"/>--
</select>月
<select id="birDay" onFocus="DaySet()">
<option value="--"/>--
</select>日
</p>
<p style="padding-left:5em;">
<input type="button" name="button" id="name" value="马上注册"/>
</p>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: