入力 (type="button") を生成し、onclick-Event を関数に設定して、パラメーターを渡す必要があります。オブジェクト全体を div に追加する必要があります。基本的にこれは私の試みですが、なぜうまくいかないのかわかりません。
コードを jsfiddle に貼り付けたので、簡単に再現できます。ここをクリックしてください。
私は何を間違っていますか?試行錯誤しながら学んでいるので、何が悪いのか説明してください。どうもありがとう!
[編集] jsfiddle がいつかダウンする場合のために、ここに私が実行しようとしたコードがあります... :)
<!doctype html>
<html>
<head>
<title>onclick event example</title>
<script type="text/javascript" language="javascript">
var i = 0;
var h = new Array();
function addButton() {
i++;
var container = document.getElementById("check0");
var h[i] = document.createElement("input");
h[i].type = 'button';
h[i].name = 'number' + i;
h[i].value = "number" + i;
h[i].id = 'number' + i;
h[i].onclick = function() {
showAlert(i)
};
container.appendChild(h[i]);
}
function showAlert(number) {
alert("You clicked Button " + number);
}
</script>
</head>
<body>
<div id="check0">
<input type="button" value="klick mich" id="number0" onclick="addButton()"/>
</div>
</body>
</html>