我是JS的新手,我需要帮助为我们点击Canvas上的图像添加随机生成的ID。 Canvas上出现的这些图像必须分配一个ID。谁能给我建议?
在HTML上:
<FORM>
<INPUT type="button" value="+"onClick="Add(new IMGid)">
<input type="text" id="amount" value=0 maxlength="3" size="1" onKeyPress="return numeralsOnly(event)">
<INPUT type="button" value="-" onClick="Remove()">
</FORM>
在Javascript文件中:
function Add(new IMGid) {
newIMG = document.createElement("IMG");
newIMG.id = "IMG" + newIMGid;
newIMG = document.getElementById("divIMG1").appendChild(newIMG);
var uniqueNum = Math.floor(Math.random()*99999);
var newIMGid = newImg.attr('uniqueNum');
if (document.getElementById('amount').value < 100){
document.getElementById('amount').value++;
newIMG = document.createElement("IMG");
x = Math.random() * 200;
y = Math.random() * 160;
c5_context.drawImage(bluefish, x, y, 58, 100).value++;
//added by Luke
a = document.createElement("IMG").setID;
Element.attr('IMG', 'random');
//--------------------
document.getElementById("imagedest").appendChild(newIMG);
c5_context.drawImage(anchovies, x, y, 58, 100).value++;
document.write(newIMG);
}
}
答案 0 :(得分:1)
function Add(new IMGid) {
- 语法错误,额外空间?
newIMG.setAttribute('id', "IMG" + newIMGid)
可能会更好一点......
var newIMGid = newImg.attr('uniqueNum');
newIMG
吗?attr
函数......你使用jquery还是什么?这里有很多错误......它应该如何工作?