| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 602 人关注过本帖
标题:大家好,我初学Javascript,这儿有一串代码,里面有一段看不懂,麻烦大家帮 ...
只看楼主 加入收藏
LIU61ZHU61ZH
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:27
专家分:0
注 册:2010-9-30
结帖率:87.5%
收藏
已结贴  问题点数:20 回复次数:2 
大家好,我初学Javascript,这儿有一串代码,里面有一段看不懂,麻烦大家帮一下,先谢了
程序代码:
<html>
<head>
<title>自定义对象示例</title>
<style type="text/css">
<!--
body {
    font-family: "宋体";
    line-height: 20px;
    text-align: center;
    background-color:#9acdcd;
}
table {
    font-size: 12px;
    text-align:center
}
.title {
    color: #FFFFFF;
    text-align: center;
    font-family: "宋体";
    font-size: 14px;
    line-height: 25px;
    font-weight: bold;
}
.blue {
    color: blue;
    font-size: 12px;
}
    #Text1
    {
        width: 203px;
    }
    #Button1
    {
        width: 62px;
    }
-->
</style>
</head>
<body>
<script type="text/javascript">
Computation=function(){};
Computation.prototype={
  Factorial:function(num)
  {
   var rs=1;
   for(i=1;i<=num;i++)
   {
    rs*=i;
   }
   return rs;
  },
   cubic:function(num)
   {
    var rs;
    rs=Math.pow(num,3);
    return rs;
   }

 }
function IsNum(num)
{
var reNum=/^\d*$/;
return(reNum.test(num));
}
function getResult(num)
{
   if(IsNum(num))
   {
    var chk= document.getElementsByName('operation');
    var op="";
    for(var i = 0; i< chk.length; i++)
    {
     if(chk[i].checked)
     {
      op=chk[i].value
     }
    }
    switch(op)
    {
    case "阶乘":
    res=Computation.prototype.Factorial(num);
    alert(num+"的阶乘为"+res);
    break;
    case "三次方":
    res=Computation.prototype.cubic(num);
    alert(num+"的三次方结果为"+res);
    break;
    }
    }
    else
    alert(num+"不是数值类型");
}

 </script>
    <table style="width:100%;">
        <tr>
            <td style="font-size: 18px">
                请输入操作数:<input id="Text1" type="text" /></td>
        </tr>
        <tr>
            <td>
                <input id="Radio1" name="operation" type="radio" value="阶乘" />阶乘
                <input id="Radio2" name="operation" type="radio" value="三次方" />三次方</td>
        </tr>
        <tr>
            <td>
                <input id="Button1" type="button" value="确定" onclick="getResult(Text1.value)" 

/></td>
        </tr>
    </table>
</body>
</html>


问题:这段for语句是什么意思,在这段代码中启了什么作用啊

for(var i = 0; i< chk.length; i++)
    {
     if(chk[i].checked)
     {
      op=chk[i].value
     }
搜索更多相关主题的帖子: Javascript 麻烦 初学 代码 
2010-09-30 20:46
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:20 
首先看第一句:(注意红色标识部分)
1.  <input id="Radio1" name="operation" type="radio" value="阶乘" />阶乘
                <input id="Radio2" name="operation" type="radio" value="三次方" />三次方</td>
2.var chk= document.getElementsByName('operation');//得到关于operation的对象数组
3.for(var i = 0; i< chk.length; i++)    //遍历数据
    {
     if(chk[i].checked)                //确认选择的哪个单选按钮(阶乘或三次方)
     {
      op=chk[i].value                  //得到当前选择的按钮的值
     }
4.  switch(op)    //根据op的值,进行处理
    {
    case "阶乘":
    res=Computation.prototype.Factorial(num);
    alert(num+"的阶乘为"+res);
    break;
    case "三次方":
    res=Computation.prototype.cubic(num);
    alert(num+"的三次方结果为"+res);
    break;
    }

C#超级群 74862681,欢迎大家的到来!
2010-09-30 22:47
LIU61ZHU61ZH
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:27
专家分:0
注 册:2010-9-30
收藏
得分:0 
恩,知道了,谢了
2010-10-02 14:16
快速回复:大家好,我初学Javascript,这儿有一串代码,里面有一段看不懂,麻烦大 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015669 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved