<script type="text/javascript">
var n = window.prompt("请输入三角形的高度", 5)
for (var i = 0i <ni++) {
for (var j = 0j <ij++) { document.write("<span style='width:10px'></span>")} //j是空白区域
for (var k = 0k <n - jk++) { document.write("<span style='width:10px'>*</span>")} //k是星号区域
document.write("</br>")//执行完一排就换行
}
</script>
<script type="text/javascript">function printTri(x)
{
//放进段落,段落居中,自然就居中了
document.write("<p align='center'>")
for (var i = 1 i <= x i++)
{
var arr = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
var pos = 0
for (var j = 1 j <= x - i + 1 j++)
{
document.write(" ")
pos = Math.round(Math.random() * (arr.length - 1))
var icon = arr[pos]
document.write(" " + icon +" ")
}
document.write("<br>")
}
document.write("</p>")
}
printTri(10) </script>
可以使用以上代码实现,请采纳。
效果如下:
<!DOCTYPE html><html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=utf-8" />
<title>等腰三角形</title>
</head>
<body>
<input type="text" name="" id="productName">
<input type="button" value="啊哈" name="btn" onclick="Triangle()">
<div id="test"></div>
<script type="text/javascript">
function Triangle() {
var length = document.getElementById("productName").value//10
var content = ""
for (var i = 0 i < length i++) {
for (var d = 0 d < length - i d++) {
content = content + "&nbsp&nbsp&nbsp"
//document.write("&nbsp&nbsp&nbsp")
}
for (var e = 0 e < i * 2 + 1 e++) {
content = content + "*"
//document.write("*")
}
content = content + "<br/>"
//document.write("<br/>")
}
document.getElementById("test").innerHTML = content
}
</script>
</body>
</html>
展示图片