【发布时间】:2013-09-01 05:04:25
【问题描述】:
我无法在 mousedownevent 上生成 1-100 的 10 个随机整数。另外我需要在表格行中显示每个,我是计算机编程的新手,我不知道我犯了什么错误。
这是我的代码:
function process() {
'use strict';
var ara = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var index;
var output = '<tr>';
for (var i = 0; i < 1 0; i++) {
index = Math.floor(Math.random() * 100);
output += '<td>' + ara[index] + '</td>';
}
output += '</tr>';
document.getElementById('numbers').innerHtML = output;
}
function init() {
'use strict';
document.getElementById('showarray').onmousedown = process;
} // End of init() function
window.onload = init;
ID号是一个表格标签 id 显示数组是我必须单击才能获得 10 个整数的 H3 标记
这是 HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>assignment2.html</title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<!-- assignment2.html -->
<h2>10 Random Integers from 1 to 100</h2>
<h3 id='showarray'>Mouse down here to show integers in table below</h3>
<table id="numbers" border="2"></table>
<span id="show5th">The fifth element is ?</span>
<script src="js/assignment2.js"></script>
</body>
</html>
jsfiddle 与 10 和 innerHTML 固定
【问题讨论】:
-
真的是
i < 1 0 ;1 和0 之间有空格吗?因为有了空间,那不是十,而是一和零。将其设为10 -
您也可以发布 HTML 吗?您可以点击编辑,然后复制/粘贴。
-
innerHtML 中的 t 是小写的。这也可能是个问题。
-
感谢您的回答。
-
好的,我发布了 HTML,我修复了你们告诉我的错误,但仍然无法正常工作。
标签: javascript arrays random dom-events