document.write不会显示输出

时间:2014-07-06 14:17:47

标签: javascript

当我写数组元素时,当我点击asending并且desending没有输出时将不会显示输出....请检查我的查询是否有错...我认为当我点击按钮它时功能停止工作什么都没有告诉我

     <html>
   <TITLE>Javascript Arrays</TITLE> 
   <BODY bgcolor="#F0F0F0">
   <h1>Implementation of Sort and Reverse Functions </h>     
     <h2>Enter number of Elements</h2>
      <form name="Anonymous Ghost">
     <INPUT type="number" name="takeinput" id="valueField" size="30">
       <Input type="button" name="name1" value="Take Input" onClick="a()"> </form>
     <form name="Anonymous Ghost">
     <Input type="button" name="name2" value="Ascending" onClick="b()">
     <Input type="button" name="name3" value="Descending" onClick="c()">
      </form>

          </BODY>
        <SCRIPT>
     function a() {
         var val = parseInt(document.getElementById('valueField').value)
             var words = new Array(val);
          for (k = 0; k < words.length; k = k + 1) {
    words[k] = window.prompt("Enter String # " + k, "");
           }
          }

        function b() {
       words.sort( ) ;
       document.write( "SORTED WORDS:" + "<BR>" ) ;
      for ( k = 0 ; k < words.length ; k = k + 1 ) {
      document.write( words[ k ] + "<BR>" ) ;
       }
      }

         function c() {
       words.sort( ) ;
        words.reverse( ) ;
       document.write( "SORTED WORDS:" + "<BR>" ) ;
       for ( k = 0 ; k < words.length ; k = k + 1 ) {
         document.write( words[ k ] + "<BR>" ) ;
         }
         }
         </SCRIPT>
       </HEAD>
        <BODY>
      </BODY>
             </HTML>

1 个答案:

答案 0 :(得分:0)

您正在函数words范围内定义a(),因此无法访问该范围。 修改在全局范围内定义words的代码,并删除函数var中的a()关键字,使其不会覆盖全局words

您的代码应该是这样的:

    <html>
    <head>
        <title>Javascript Arrays</title> 
    </head> 
    <body bgcolor="#F0F0F0">
        <h1>Implementation of Sort and Reverse Functions </h1>     
        <h2>Enter number of Elements</h2>
        <form name="Anonymous Ghost">
        <INPUT type="number" name="takeinput" id="valueField" size="30" />
        <Input type="button" name="name1" value="Take Input" onClick="a()" />
        </form>
        <form name="Anonymous Ghost">
        <Input type="button" name="name2" value="Ascending" onClick="b()" />
        <Input type="button" name="name3" value="Descending" onClick="c()" />
        </form>
    </body>
    <script>
        var words;
        function a() {
            var val = parseInt(document.getElementById('valueField').value);
            words = new Array(val);
            for (k = 0; k < words.length; k = k + 1) {
                words[k] = window.prompt("Enter String # " + k, "");
            }
        }

        function b() {
            words.sort( ) ;
            document.write( "SORTED WORDS:" + "<BR>" ) ;
            for ( k = 0 ; k < words.length ; k = k + 1 ) {
                document.write( words[ k ] + "<BR>" ) ;
            }
        }

        function c() {
            words.sort( ) ;
            words.reverse( ) ;
            document.write( "SORTED WORDS:" + "<BR>" ) ;
            for ( k = 0 ; k < words.length ; k = k + 1 ) {
                document.write( words[ k ] + "<BR>" ) ;
            }
        }
    </script>
    </html>

PS:您可以在浏览器中使用开发人员工具来检查Javascripts控制台中的错误。

PS2:保持你的代码整洁干净。