function createDiv(nameId,width,height,color)
{
try
{
// Initialize And Create Div
if(document.createElement('div'))
{
// document.write("<br>Create Div Success");
if((nameId.length!=0) && (width>0) &&(height>0) && (color.length!=0))
{
//creating div
var divCreate = document.createElement('div');
divCreate.id=nameId;
document.getElementById(nameId).width = width;
document.getElementById(nameId).height = height;
document.getElementById(nameId).background = color;
document.getElementById(nameId).backgroundColor = "Red";
document.body.appendChild(divCreate);
document.write(" <br>Create Div Success And Added To Body");
// document.write(" <br>Create Div Success And Added To Body");
}
else
{
// document.write("<br>All Field Value Required");
}
}
else
{
document.write("<br>Unable To Create Div");
}
}
catch(e)
{
document.write("<br>"+e);
}
}
document.getElementById()
получает Null
. Я пытаюсь создать DIV с помощью javascript и назначить ему ширину/высоту и цвет, но он не работает. Однако, если я использую код ниже, создается div, но он не отображается на экране. И если я использую функцию createDiv()
, она не работает.document.getElementById() получает Null
divCreate.id=nameId;
divCreate.width = width;
divCreate.height = height;
divCreate.background = color;
divCreate.backgroundColor = "Red";
document.body.appendChild(divCreate);
Читайте здесь: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – elclanrs