<html>
<body>
<input type="text" id="number"/>
<input type="button" value="create button" onclick="createbtn()"/>
<br>
<script>
function createbtn()
{
var n=document.getElementById("number").value;
for(i=1;i<=n;i++)
{
var x = document.createElement("INPUT");
x.setAttribute("type", "button");
x.setAttribute("value", i);
x.setAttribute("id","x"+i);
document.body.appendChild(x);
document.getElementById("x"+i).onclick=function(){rotate(i)};
}
}
function rotate(p)
{
var n=document.getElementById("number").value;
var j=n;
var k=0;
for(i=n;i>p;i--)
{
document.getElementById("x"+i).value=i-p;
}
for(i=1;i<=p;i++)
{
document.getElementById("x"+i).value=(j-(p-1))+k;
k++;
}
}
</script>
</body>
</html>
私がしようとしているのは、テキストフィールドに入力された数字に等しい数のボタンを作成し、それらのボタンにオンクリックイベントを追加することで、各ボタンの値が回転した。私がonclickイベントを取得するために使用したメソッドが正しく動作していません。助けて!!javascriptのボタンにクリックイベントを追加するには?
:
すべてのすべてで、これを試してみてください。コードに関するhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Closuresを読んでください。 –