王小米高
2017-01-18 22:47
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数组</title>
<script type="text/javascript">
var myarr=['*','**','***','****'];
var length=myarr.length;
for(var i=0;i<length;i++)
document.write(myarr[i]+"</br>");
</div>
function showLen()
{
alert("数组长度为"+length);
}
//将数组内容输出,完成达到的效果。
</script>
</head>
<body>
<form>
<input type="button" onclick="showLen()" value="数组的长度">
</form>
</body>
</html>
你代码里13行有错误,多了个‘</div>’,我给你改了,另外我自己写的实现不知道符合你的需求不。
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>数组</title> <script type="text/javascript"> var myarr = ['*', '**', '***', '****']; var length = myarr.length; for (var i = 0; i < length; i++) { document.write('<span>' + myarr[i] + "</br></span>"); } function showLen() { alert("数组长度为" + length); } setTimeout(function() { var d = document; var s = true; var spans = d.querySelectorAll('span'); var f = d.querySelector('form'); var btn = d.createElement('input'); btn.setAttribute('type', 'button'); btn.setAttribute('value', '清除/显示'); f.appendChild(btn); btn.addEventListener('click', function() { if (s == true) { for (var i = 0; i < spans.length; i++) { spans[i].style.display = 'none'; } s = !s; } else { for (var i = 0; i < spans.length; i++) { spans[i].style.display = 'block'; } s = !s; } }) }, 300); </script> </head> <body> <form> <input type="button" onclick="showLen()" value="数组的长度"> </form> </body> </html>
JavaScript进阶篇
468195 学习 · 21891 问题
相似问题