⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 day4_13.html

📁 不错的javascript学习资料适合于刚刚学习javascirpt的出学者
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    <td colspan="2">-<a href="../../1-teach/other/index.htm">其它</a> </td>
  </tr>
  <tr> 
    <td colspan="2" bgcolor="#666699"> 
      <div align="center"><font color="#FFFFFF">更多教程</font></div>
    </td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2" height="17"><a href="../../1hdml/index.html">-HDML</a></td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2" height="23"><font face="宋体"><a href="../../1-backend/database/course/day1_1.html">-网络数据库</a></font></td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2" height="14"><a href="../../1-backend/protocols/ping/index.html"><font face="arial, helvetica, sans-serif">-ping</font></a></td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2" height="20"><a href="../../1-backend/cgi_perl/search_engine/index.html">-创建搜索引擎</a></td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2" height="16">-<a href="../../1adobe/GoLive/index.html">Adobe GoLive</a></td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2"><a href="../../1-backend/cgi_perl/templates/index.html">-模板</a></td>
  </tr>
  <tr bgcolor="#666699"> 
    <td colspan="2" align="center"><font color="#FFFFFF">合作伙伴</font></td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2" align="left">-<a href="http://www.5dmedia.com/" target="_blank">5D精英网</a></td>
  </tr>
  <tr align="center" bgcolor="#FFFFFF"> 
    <td colspan="2"> <img src="../../Library/front_monkey.gif" width="59" height="68"></td>
  </tr>
  </tbody> 
</table>
<!-- #EndLibraryItem --> </td>
 <!-- End of headlines (column 1: left column) --> <!-- Gutter between columns 1 and 2 --> 
 <td width="10" height="794"><img src="http://www.sohu.com/images/pixel.gif" width=10></td>
 <!-- Search box and directories (columns 2 and 3: middle columns, combined into one) --> 
 <td align=center valign=top width="558"> 
 <div align="left"><!-- #BeginEditable "1" --> 
<title>JavaScrip教程</title>
 <strong><font face="宋体" size="3">第十三页:</font><font size="3">参数及返回值</font></strong><nobr> 
      <p align="JUSTIFY"><font size="2"></font><font face="宋体" size="3">尽管无参数的函数在减少写源码工作量,HTML源码可读性上很<br>
        有用,但有参数的函数会更为有用。</font></p>
      <font size="3"> 
      <p align="JUSTIFY"> <font size="2"></font>上一例中,当返回的分、秒值小于<font face="宋体"><big>10</big></font>时会有问题发生。我们想<br>
        要看到的秒值是<font face="宋体"><big>04</big></font>而非<font face="宋体"><big>4</big></font>。我们可以这样做:</p>
      </font></nobr> 
      <p align="JUSTIFY"><font face="宋体"><br>
        var the_minute = the_date.getMinutes();<br>
        <br>
        if (the_minute &lt; 10) <br>
        <br>
        {<br>
        <br>
        the_minute = &quot;0&quot; + the_minute;<br>
        <br>
        }<br>
        <br>
        <br>
        <br>
        var the_second = the_date.getSeconds();<br>
        <br>
        if (the_second &lt; 10)<br>
        <br>
        {<br>
        <br>
        the_second = &quot;0&quot; + the_second;<br>
        <br>
        }<br>
        <br>
        </font><nobr><font size="3">它会非常有效。但是注意,同样的源码你写了两次:若某件<br>
        东西小于<font face="宋体">10</font></font><font size="2">,</font><font size="3">则前面加“<font face="宋体">0</font>”。所以要考虑当用同一代码要多<br>
        次重写时,用函数来做。本例中我写了一个叫</font><font face="宋体" size="3">fixNumber</font><font size="3">的函数:</font></nobr></p>
      <p align="JUSTIFY"><font face="宋体"><br>
        function fixNumber(the_number)<br>
        <br>
        {<br>
        <br>
        if (the_number &lt; 10)<br>
        <br>
        {<br>
        <br>
        the_number = &quot;0&quot; + the_number;<br>
        <br>
        }<br>
        <br>
        return the_number;<br>
        <br>
        }<br>
        <br>
        </font><font face="宋体" size="3"><tt>fixNumber</tt>的参数是<tt>the_number</tt>。一个参数也是一个变量,当该<br>
        函数被调用时,其参数值也被设置。在本例中,我们这样调用<br>
        函数: </font></p>
      <p align="JUSTIFY"><font face="宋体"><br>
        var fixed_variable = fixNumber(4);<br>
        <br>
        </font><font face="宋体" size="3">参数<tt>the_number</tt>在函数中设置为<tt>4</tt>。到现在你应该对<tt>fixNumber</tt><br>
        的主体有了一定的了解。它的意思是:如果变量<tt>the_number</tt>小<br>
        于10,则在它的前面加一个0。这里面新的内容是<tt>return</tt>指令:<br>
        返回<tt>the_number</tt>的值。在下面的情况中就会用到return指令: </font></p>
      <p align="JUSTIFY"><font face="宋体"><br>
        var some_variable = someFunction();<br>
        <br>
        </font><font face="宋体" size="3">变量<tt>some_variable</tt>的值是<tt>函数someFunction()</tt> 
        的返回值。<br>
        在<tt>fixNumber</tt>中,我加入: return <tt>the_number</tt>,则退出函数并<br>
        将返回<tt>the_number</tt>的值返回给任何一个等待被设置的变量。<br>
        所以,我这样书写代码:</font></p>
      <p align="JUSTIFY"><font face="宋体">var fixed_variable = fixNumber(4);<br>
        </font></p>
      <p><font size="3"><font face="宋体"><tt>the_number</tt></font>的初始值将通过函数调用被设置为<font face="宋体">4</font>,然后由于<font face="宋体">4</font>小<br>
        于<font face="宋体">10</font>,所以<font face="宋体"><tt>the_number</tt></font>将被改为<font face="宋体">&quot;<tt>04</tt>&quot;</font>。然后<font face="宋体"><tt>the_number</tt></font>值被<br>
        返回,而且变量<font face="宋体"><tt>fixed_variable</tt></font>将被设置为<font face="宋体">&quot;04&quot; 
        </font>。</font></p>
      <p> <font size="3"></font><font face="宋体" size="3">为了将<tt>fixNumber</tt>包括在原始函数<tt>announceTime()</tt>中,我添加了<br>
        如下内容:</font></p>
      <p><font face="宋体">function announceTime()<br>
        <br>
        {<br>
        <br>
        //get the date, the hour, minutes, and seconds<br>
        <br>
        var the_date = new Date();<br>
        <br>
        var the_hour = the_date.getHours();<br>
        <br>
        var the_minute = the_date.getMinutes();<br>
        <br>
        var fixed_minute = fixNumber(the_minute);<br>
        <br>
        var the_second = the_date.getSeconds();<br>
        <br>
        var fixed_second = fixNumber(the_second);<br>
        <br>
        <br>
        //put together the string and alert with it<br>
        <br>
        var the_time = the_hour + &quot;:&quot; + fixed_minute + &quot;:&quot; 
        + fixed_second;<br>
        <br>
        alert(&quot;The time is now: &quot; +the_time);<br>
        <br>
        } <br>
        <br>
        </font><font face="宋体" size="3">假定时间链接被点击时,时间为12:04:05。用<tt>new Date()</tt>获得<br>
        日期,用<tt>getHours()</tt>获得小时,用前面所属方法获得分钟,分<br>
        钟在本例中应该是<tt>4</tt>, 然后调用<tt>fixNumber</tt>,其参数为<tt>the_minute</tt>:</font></p>
      <p><font face="宋体"><br>
        var fixed_minute = fixNumber(the_minute);<br>
        <br>
        </font></p>
      <p><font size="3">当<font face="宋体"><tt>fixNumber()</tt></font>被调用时,参数<font face="宋体"><tt>the_number</tt></font>被设置为<font face="宋体"><tt>the_minute</tt></font>。<br>
        在本例中由于<font face="宋体"><tt>the_minute</tt></font>是<font face="宋体">4</font>,所以<font face="宋体"><tt>the_number</tt></font>将被设置为<font face="宋体">4</font>。<br>
        设置完参数后,我们进入函数主体。由于<font face="宋体">4</font>小于<font face="宋体">10</font>,<font face="宋体"><tt>the_number</tt></font><br>
        被改变为<font face="宋体">&quot;<tt>04</tt>&quot;</font>,然后<font face="宋体"><tt>the_number</tt></font>值用<font face="宋体">return</font>指令返回。当<font face="宋体">&quot;04&quot;</font><br>
        被<font face="宋体"><tt>fixNumber</tt></font>返回后,本例<tt><font face="宋体">fixed_minute</font></tt>就等于<font face="宋体">&quot;04&quot;</font>。<font face="宋体"> 
        </font></font></p>
      <font size="3"> 
      <p>我们一步一步来研究该过程。假定时间为<font face="宋体">12:04:05</font>。
      </font> 
      <ol>
        <font size="3"> 
        <h2>我们从函数<tt><font face="宋体">announceTime()</font>开始</tt></h2>
        <li><font face="宋体"><tt>the_minute</tt> = <tt>the_date.getMinutes();</tt></font>则<font face="宋体"><tt>the_minute</tt> 
          = 4 </font></li>
        <li><tt><font face="宋体">fixed_minute = fixNumber(the_minute);</font></tt>等于<tt>函<br>
          数<font face="宋体">fixNumber()</font></tt>并将其值返回给<font face="宋体"><tt>fixed_minute</tt> 
          </font> 
          <h2>现在进入函数<font face="宋体"><tt>fixNumber()</tt></font></h2>
        </li>
        <li><tt>函数<font face="宋体">fixNumber(the_number)fixNumber()</font></tt>用<font face="宋体"><tt>the_minute</tt></font>的<br>
          值调用,<font face="宋体">the_minute</font>值是<font face="宋体">4</font>,所以现在<font face="宋体"><tt>the_number 
          = 4</tt> </font></li>
        <li><tt>如果<font face="宋体">(the_number &lt; 10) {the_number = &quot;0&quot; 
          +<br>
          the_number;}</font></tt>由于<font face="宋体">4</font>小于<font face="宋体">10</font>,所以<font face="宋体"><tt>the_number</tt></font>现在等于<font face="宋体">&quot;04&quot;</font></li>
        <li>返回<font face="宋体"><tt>the_number</tt></font>值<font face="宋体">,</font>退出该函数并返回值<font face="宋体">&quot;04&quot; 
          </font> 
          <p><tt>现在已经退出函数<font face="宋体">fixTime()</font></tt>,所以现在我们回<br>
            到<font face="宋体"><tt>announceTime()</tt></font></p>
        </li>
        <li>该函数返回值为<font face="宋体">&quot;04&quot;</font>,所以<font face="宋体"><tt>fixed_minute</tt> 
          </font>现在等于<font face="宋体">&quot;04&quot; </font> 
          <p>该例用了一个只有一个参数的函数。实际上你可以为函数<br>
            设置多个参数。例如<a href="day4_14.html">该函数输入两个数组并返回一个元素<br>
            列表</a>。</p>
        </li>
        </font> 
      </ol>
      <p align="left"><font face="宋体" size="3" color="#000000"><strong>JavaScript教程</strong></font><font color="#FF0000" face="宋体" size="3"><br>
        </font><font size="3" color="#FF0000">第一页</font><font size="2" face="宋体"> 
        </font><a href="day4_13.html"><font size="3">第四课介绍</font></a><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第二页</font> <a href="day4_2.html">循环介绍</a></font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第三页</font> <a href="day4_3.html">循环的密码</a></font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第四页</font> <a href="day4_4.html">再谈<font face="宋体">WHILE</font>循环</a></font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第五页</font> <a href="day4_5.html">For循环</a></font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第六页</font> <a href="day4_6.html">嵌套循环</a></font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第七页</font> <a href="day4_7.html">循环练习</a></font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第八页</font> <a href="day4_8.html">数组</a></font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第九页</font> <a href="day4_9.html">数组和循环</a></font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第十页</font> <a href="day4_10.html">文件目标模块中的数组</a></font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第十一页</font> <a href="day4_11.html">函数</a></font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第十二页</font> <a href="day4_12.html">无参数函数</a></font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第十三页</font> 参数及返回值</font><font size="2"><br>
        </font><font size="3"><font color="#FF0000">第十四页</font> <a href="day4_14.html">多于一个参数的函数</a></font></p>
      <p align="left"><font face="宋体" size="3">[<a href="index.html">第1课</a>][<a href="day2_1.html">第2课</a>][<a href="day3_1.html">第3课</a>][第4课][<a href="day5_1.html">第5课</a>]</font></p>
      <!-- #EndEditable --></div>
 </td>
 <!-- End of search box and directories (columns 2 and 3: middle columns, combined into one) --> 
 <!-- Gutter between columns 3 and 4 --> <!-- Other stuff (column 4: right column) --> 
 <!-- End of other stuff (column 4: right column) --> </tr>
 </table>
<!-- End of table surrounding page contents -->

 <hr noshade size=1 width=700>
 <span class=eng><br>
 Copyright (C) 1998-2000 Internet Technologies China.&nbsp; All rights reserved. 
 </span> 
</center>
</body>
<!-- #EndTemplate --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -