Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
function atira(x0,y0,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6){
var t = [], att = [], attt = [];
for(var i=0; i<7; i++){
t[i] = document.createElement("div");
att[i] = document.createAttribute("class");
att[i].value = "tiroJog";
t[i] = setAttributeNode(att[i]);
attt[i] = document.createAttribute("style");
attt[i].value = "top:"+y[i]+"px;left:"+x[i]+"px";
t[i].setAttributeNode(attt[i]);
document.body.appendChild(t[i]);
}
}
Boa noite, estou estudando javascript e implementando meus conhecimentos tentando criar jogos, ai me deparei com esse seguinte erro, vi no console e esta dando o seguinte erro setAttributeNode is not defined, ja tentei colocar as variaveis fora da funçao mas acho que isso não tem nada a ver pq só uso essas variaves nessa funçao, estou tentando a um tempao entender o motivo do erro, se alguem puder me explicar fico muito grato!?@Maujor
Funciono a primeira linda da class, ja a do style não funciono, msm eu tirando a linha do style cria um style altomatico com apenas o top, isso é normal?
Sem o for funciona normal mas o código fica muito grande ...
function atira(x,y,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6){
var t = document.createElement("div");
var t1 = document.createElement("div");
var t2 = document.createElement("div");
var t3 = document.createElement("div");
var t4 = document.createElement("div");
var t5 = document.createElement("div");
var t6 = document.createElement("div");
var att1 = document.createAttribute("class");
var att2 = document.createAttribute("class");
var att3 = document.createAttribute("class");
var att4 = document.createAttribute("class");
var att5 = document.createAttribute("class");
var att6 = document.createAttribute("class");
var att7 = document.createAttribute("class");
att1.value = "tiroJog";
att2.value = "tiroJog";
att3.value = "tiroJog";
att4.value = "tiroJog";
att5.value = "tiroJog";
att6.value = "tiroJog";
att7.value = "tiroJog";
t.setAttributeNode(att1);
t1.setAttributeNode(att2);
t2.setAttributeNode(att3);
t3.setAttributeNode(att4);
t4.setAttributeNode(att5);
t5.setAttributeNode(att6);
t6.setAttributeNode(att7);
var attt1 = document.createAttribute("style");
var attt2 = document.createAttribute("style");
var attt3 = document.createAttribute("style");
var attt4 = document.createAttribute("style");
var attt5 = document.createAttribute("style");
var attt6 = document.createAttribute("style");
var attt7 = document.createAttribute("style");
attt1.value = "top:"+y+"px;left:"+x+"px";
attt2.value = "top:"+y1+"px;left:"+x1+"px";
attt3.value = "top:"+y2+"px;left:"+x2+"px";
attt4.value = "top:"+y3+"px;left:"+x3+"px";
attt5.value = "top:"+y4+"px;left:"+x4+"px";
attt6.value = "top:"+y5+"px;left:"+x5+"px";
attt7.value = "top:"+y6+"px;left:"+x6+"px";
t.setAttributeNode(attt1);
t1.setAttributeNode(attt2);
t2.setAttributeNode(attt3);
t3.setAttributeNode(attt4);
t4.setAttributeNode(attt5);
t5.setAttributeNode(attt6);
t6.setAttributeNode(attt7);
document.body.appendChild(t);
document.body.appendChild(t1);
document.body.appendChild(t2);
document.body.appendChild(t3);
document.body.appendChild(t4);
document.body.appendChild(t5);
document.body.appendChild(t6);
}@programadorweb2016
Por que continua usando setAttributeNode?
Faça as alterações que eu indiquei e vai funcionar.
Estude seu script em:
https://jsfiddle.net/maujor/yhsbnL4m/85/
@Maujor
Agora deu certo, eu tinha tentado daquela forma acima que tinha me passado, mas nao estava dando acho que era por causa das aspas duplas, as var x y dava indefinido tb, acho que era pq nao tinha nenhum valor no array delas, agora funciono perfeitamente e ainda por cima me fez aprender coisas novas, muito obrigado por essa incrivel experiencia
@programadorweb2016
Em lugar de usar setAttributeNode use o método setAttribute cuja sintaxe é conforme a seguir: