__ZERO_UNO__
28-08-2011, 14:30
Ciao, sono da poco alle prese con Javascript e alcuni problemi che possono sembrare banali sono difficilmente valicabili per me. :)
Il problema è che la funzione che trovate in basso non rimpiazza l'elemento old ma ne aggiunge un'altro. Ho provato anche a cancellare l'elemento vecchio e appendere quello nuovo ma non funziona. Che posso fare?
Grazie mille.
PS Lo script è posto alla fine di body
Tableau.prototype.go = function(resultPlaceId)
{
var resultPlace = document.getElementById(resultPlaceId);
if (typeof resultPlace == undefined) return null;
var res = this.solve();
var str = 'Optimum found';
var div = document.createElement('div');
div.setAttribute('id', 'step_panel');
if (res == undefined)
{
h3 = document.createElement('h3');
h3.innerHTML = 'Wrong input!!';
h3.setAttribute('id', 'result_place');
div.appendChild(h3);
if (old == null)
resultPlace.appendChild(div);
else
{
old = old.parentNode.replaceChild(div, old);
}
return;
}
if (res == false)
str = 'Unlimited';
for (var s = 0; s < this.steps.length; s++)
{
div.appendChild(this.steps[s]);
}
var h3 = document.createElement('h3');
h3.setAttribute('id', 'result_place');
h3.innerHTML = str;
div.appendChild(h3);
var old = document.getElementById('step_panel');
if (old == null)
resultPlace.appendChild(div);
else
{
old = old.parentNode.replaceChild(div, old);
}
}
Il problema è che la funzione che trovate in basso non rimpiazza l'elemento old ma ne aggiunge un'altro. Ho provato anche a cancellare l'elemento vecchio e appendere quello nuovo ma non funziona. Che posso fare?
Grazie mille.
PS Lo script è posto alla fine di body
Tableau.prototype.go = function(resultPlaceId)
{
var resultPlace = document.getElementById(resultPlaceId);
if (typeof resultPlace == undefined) return null;
var res = this.solve();
var str = 'Optimum found';
var div = document.createElement('div');
div.setAttribute('id', 'step_panel');
if (res == undefined)
{
h3 = document.createElement('h3');
h3.innerHTML = 'Wrong input!!';
h3.setAttribute('id', 'result_place');
div.appendChild(h3);
if (old == null)
resultPlace.appendChild(div);
else
{
old = old.parentNode.replaceChild(div, old);
}
return;
}
if (res == false)
str = 'Unlimited';
for (var s = 0; s < this.steps.length; s++)
{
div.appendChild(this.steps[s]);
}
var h3 = document.createElement('h3');
h3.setAttribute('id', 'result_place');
h3.innerHTML = str;
div.appendChild(h3);
var old = document.getElementById('step_panel');
if (old == null)
resultPlace.appendChild(div);
else
{
old = old.parentNode.replaceChild(div, old);
}
}