JavaScript客户端脚本语言的应用

本文通过三个实例介绍了JavaScript在客户端脚本中的应用:制作简易计算器页面、实现树形菜单交互以及用户注册表单的客户端校验。讨论了BOM和DOM在JavaScript中的角色,BOM用于浏览器窗口交互,而DOM则提供了一种结构化表示HTML页面的方式。
摘要由CSDN通过智能技术生成

1.使用HTML中的表单、表格以及JavaScript客户端脚本语言实现如下图所示的简易计算器页面

在这里插入图片描述

<!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=gb2312" />
<title>简易计算器</title>
<script  type="text/javascript">
var res;var value1,value2,opt;

function count(opt)
{
    
	value1=parseInt(document.getElementById("num1").value);
  value2=parseInt(document.getElementById("num2").value);
	switch(opt)
	{
    
		case '+':res=value1+value2;break;
		case '-':res=value1-value2;break;
		case '×':res=value1*value2;break;
        default:
        res=value1/value2;
        break;
	}
	document.getElementById("num3").value=res;
}
</script>
</head>

<body>
   <form >
     <table width="356" height="272" style="text-align:center">
       <tr>
         <td width="54" height="60" ><img src="images/shop.gif" >
         </td>
         <td colspan="4"><h2  align="center" > 购物简易计算器</h2></td>
       </tr>
       <tr>
         <td height="32" ><font size="-1">第一个数</font></td>
         <td colspan="4"><input type="text" style="float:left;" size="30" id="num1"></td>
       </tr>
       <tr>
        <td ><font size="-1">第二个数</font></td>
        <td colspan="4"><input type="text" style="float:left;" size="30" id="num2"></td>
       </tr>
       <tr>
        <td><input type="button"style=" width:50px;" value="+" name="add" οnclick="count('+')"></td>
        <td width="70" ><input type="button" style=" width:50px;" value="-" name="sub" onclick="count('-')"></td>
      <td width="90"> <input type="button" style=" width:50px;" value="×" name="mul" onclick="count('×')"></td>
       <td width="50"><input type="button" style=" width:50px;" value="÷" name="div" onclick="count('÷')"></td>
       <td width="78">&nbsp;</td>
       </tr>
       <tr>
       <td ><font size="-1">计算结果</font></td>
       <td  colspan="4"><input type="text" style="float:left;" size="30"  height="20" id="num3"></td>
       </tr>
     </table>
   </form>

</body>
</html>

编程结果:
在这里插入图片描述

2. 使用项目列表制作一个如图7所示的完整的树形菜单页面treeMenu.html

使用带参数的JavaScript函数,通过参数来控制单击某个一级菜单时显示或隐藏该菜单下的二级列表。
在这里插入图片描述

<!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=gb2312" />
<title>制作树形菜单</title>
<style type="text/css">
body{
    font-size:13px;
     line-height:20px;
	 }
a{
    font-size: 13px;
  color: #000000;
  text-decoration: none;
  }
a:hover{
    font-size:13px;
        color: #ff0000;
	   }
img {
    
	vertical-align: middle;
	border:0;
}
.no_circle{
    list-style:none;
   display:none;
	}
</style>
<script type="text/javascript">
   function show(d1){
    
	   if(document.getElementById(d1).style.display=='block')
	   {
    
		   document.getElementById(d1).style.display='none'
	   }
	   else
	   {
    
		   document.getElementById(d1).style.display='block';
	    }
		
	   }
</script>

</head>

<body>
<b><img src="images/fold.gif">树形菜单:</b>  
<ul><a href="javascript:οnclick=show('art')" ><img src="images/fclose.gif">文学艺术</a></ul>
<ul id="art" class="no_circle" style="display:none;">
	<li><img src="images/doc.gif" >先锋写作</li>
    <li> <img src="images/doc.gif" >小说散文</li>
    <li><img src="images/doc.gif" >诗风词韵</li>
</ul>
<ul><a href="javascript:οnclick=show('photo')"><img src="images/fclose.gif">贴图专区</a></ul>
<ul id="photo" class="no_circle"  style="display:none;">
    <li>
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值