题目:使用JavaScript输出如图所示的倒正金字塔直线。


 编写代码实现的思路与步骤:

(1)、使用document.write()输出水平线。

(2)、使用循环控制每条水平线的长度。


 完整代码与具体代码解释:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>倒正金字塔直线</title>

    <style>

        h1{

          text-align:center;    //设置标题字体居中

          }

    </style>

</head>

<body>

    <h1>打印倒正金字塔直线</h1>

    <script>

        for(var i=10;i>-11;i--)     //使用for循环控制每条水平线的输出

        {

     document.write("<hr width=" + 10*Math.abs(i) + "%/>")  //document.write()输出水平线

        }

      </script>

</body>

</html>

更多推荐

【JavaScript实训】-- 打印倒正金字塔直线