我如何添加一个div并在以后捡起它

时间:2022-09-26 11:48:06

I think this is specific to IE 6.0 but...

我认为这是IE 6.0特有的,但......

In JavaScript I add a div to the DOM. I assign an id attribute. When I later try to pick up the div by the id all I get is null.

在JavaScript中,我向DOM添加了一个div。我分配了一个id属性。当我后来尝试通过id获取div时,我得到的是null。

Any suggestions?

Example:

var newDiv = document.createElement("DIV");
newDiv.setAttribute("ID", "obj_1000");
document.appendChild(newDiv);

alert("Added:" + newDiv.getAttribute("ID") + ":" + newDiv.id + ":" + document.getElementById("obj_1000") );

Alert prints "::null"

警报打印“:: null”

Seems to work fine in Firefox 2.0+

似乎在Firefox 2.0+中运行良好

5 个解决方案

#1


7  

In addition to what the other answers suggest (that you need to actually insert the element into the DOM for it to be found via getElementById()), you also need to use a lower-case attribute name in order for IE6 to recognize it as the id:

除了其他答案建议的内容(您需要将元素实际插入DOM以便通过getElementById()找到它)之外,您还需要使用小写的属性名称,以便IE6将其识别为id:

var newDiv = document.createElement("DIV"); 
newDiv.setAttribute("id", "obj_1000");
document.body.appendChild(newDiv);

alert("Added:"
   + newDiv.getAttribute("id") 
   + ":" + newDiv.id + ":" 
   + document.getElementById("obj_1000") );

...responds as expected:

......按预期回应:

Added:obj_1000:obj_1000:[object]

According to the MSDN documentation for setAttribute(), up to IE8 there is an optional third parameter that controls whether or not it is case sensitive with regard to the attribute name. Guess what the default is...

根据setAttribute()的MSDN文档,直到IE8,有一个可选的第三个参数,它控制它是否与属性名称区分大小写。猜猜默认是什么......

#2


3  

The div needs to be added to an element for it to be part of the document.

div需要添加到元素中才能成为文档的一部分。

document.appendChild(newDiv);

alert( document.getElementById("obj_1000") );

#3


1  

You have to add the div to the dom.

你必须将div添加到dom。

// Create the Div
var oDiv = document.createElement('div');
document.body.appendChild(oDiv);

#4


0  

newDiv.setAttribute( "ID", "obj_1000" );

newDiv.setAttribute(“ID”,“obj_1000”);

should be

newDiv.id = "obj_1000";

newDiv.id =“obj_1000”;

#5


0  

Hummm, thanks for putting me on the right track guys...this was odd but it turns out that if I change the case to lower case, everything starting working just fine...

嗯,谢谢你把我放在正确的轨道上...这很奇怪但事实证明,如果我将案例改为小写,一切都开始正常工作......

Finished Result:

var newDiv = document.createElement("DIV");
newDiv.setAttribute("id", "obj_1000");
document.appendChild(newDiv);

alert("Added:" +
      newDiv.getAttribute("id") + ":" +
      newDiv.id + ":" +
      document.getElementById("obj_1000"));

ODD...VERY ODD

#1


7  

In addition to what the other answers suggest (that you need to actually insert the element into the DOM for it to be found via getElementById()), you also need to use a lower-case attribute name in order for IE6 to recognize it as the id:

除了其他答案建议的内容(您需要将元素实际插入DOM以便通过getElementById()找到它)之外,您还需要使用小写的属性名称,以便IE6将其识别为id:

var newDiv = document.createElement("DIV"); 
newDiv.setAttribute("id", "obj_1000");
document.body.appendChild(newDiv);

alert("Added:"
   + newDiv.getAttribute("id") 
   + ":" + newDiv.id + ":" 
   + document.getElementById("obj_1000") );

...responds as expected:

......按预期回应:

Added:obj_1000:obj_1000:[object]

According to the MSDN documentation for setAttribute(), up to IE8 there is an optional third parameter that controls whether or not it is case sensitive with regard to the attribute name. Guess what the default is...

根据setAttribute()的MSDN文档,直到IE8,有一个可选的第三个参数,它控制它是否与属性名称区分大小写。猜猜默认是什么......

#2


3  

The div needs to be added to an element for it to be part of the document.

div需要添加到元素中才能成为文档的一部分。

document.appendChild(newDiv);

alert( document.getElementById("obj_1000") );

#3


1  

You have to add the div to the dom.

你必须将div添加到dom。

// Create the Div
var oDiv = document.createElement('div');
document.body.appendChild(oDiv);

#4


0  

newDiv.setAttribute( "ID", "obj_1000" );

newDiv.setAttribute(“ID”,“obj_1000”);

should be

newDiv.id = "obj_1000";

newDiv.id =“obj_1000”;

#5


0  

Hummm, thanks for putting me on the right track guys...this was odd but it turns out that if I change the case to lower case, everything starting working just fine...

嗯,谢谢你把我放在正确的轨道上...这很奇怪但事实证明,如果我将案例改为小写,一切都开始正常工作......

Finished Result:

var newDiv = document.createElement("DIV");
newDiv.setAttribute("id", "obj_1000");
document.appendChild(newDiv);

alert("Added:" +
      newDiv.getAttribute("id") + ":" +
      newDiv.id + ":" +
      document.getElementById("obj_1000"));

ODD...VERY ODD