打印空心正方形的javascript代码

解决方案 »

  1.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>new document</title>
    </head>
    <body style="background:#eee">
    <script type="text/javascript">
    (function(){
    document.write("<div style='width:200px; height:200px; border:1px solid #ff0000'></div>");
    })();
    </script>
    </body>
    </html>
      

  2.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>new document</title>
    <style type="text/css">
    *{
    line-height:8px; 
    }
    </style>
    </head>
    <body>
    <script type="text/javascript">
    (function(){
    var x = prompt("请输入正方形边长:", 10);
    x = parseInt(x, 10);
    if(isNaN(x) || x < 1 || x > 1000){
    alert("输入错误!");
    return;
    }
    var line = new Array(x + 1).join("*");
    document.writeln(line + "<br/>");
    for(var i = 0; i < x - 2; i++){
    document.writeln("*" + new Array(x - 1).join("&nbsp;") + "*<br/>");
    }
    document.writeln(line + "<br/>");
    })();
    </script>
    </body>
    </html>