javascript中 DOM编程的一个问题
为什么通过creatElement()创建的元素节点无法获取里面的属性节点和文本节点functionpreparePlaceholder(){varpara=docume...
为什么通过creatElement()创建的元素节点 无法获取里面的属性节点和文本节点
function preparePlaceholder()
{
var para = document.createElement("img");
para.setAttribute("src","4.jpg");
para.setAttribute("id","place");
para.setAttribute("title","picture name");
//var Boms = document.getElementsByTagName("body");
//Boms[0].appendChild(para);
var description = document.createElement("p");
description.setAttribute("id","description");
var txt1 = document.createTextNode("This is picture");
description.appendChild(txt1);
//Boms[0].appendChild(description);
var gallery = document.getElementById("imagegallery");
//gallery.parentNode.insertBefore(para,gallery);
//gallery.parentNode.insertBefore(description,gallery);
var gallery = document.getElementById("imagegallery");
gallery.insertAfter(place,gallery);
gallery.insertAfter(description.gallery);
}
function insertAfter(newElement,targetElement)
{
var parend = targetElement.parentNode
if(parend.lastChild == targetElement)
{
parend.appendChild(newElement);
}
else{
parend.insertBefore(newElement,parend.nextSibling);
}
}
//------------------------------------------------------------------------
function eventcoolter(fun)
{
var onrired = window.onload;
if(typeof window.onload != 'function'){
window.onload = fun;
}
else{
onrired();
fun();
}
}
//----- ----------------------------------------------------------------------
eventcoolter(preparePlaceholder())
通过document.createElement("img")创建的元素节点中的属性节点无法被获取 IE报错place未定义 可是img的id已经通过para.setAttribute("id","place");进行创建了为什么获取不到 展开
function preparePlaceholder()
{
var para = document.createElement("img");
para.setAttribute("src","4.jpg");
para.setAttribute("id","place");
para.setAttribute("title","picture name");
//var Boms = document.getElementsByTagName("body");
//Boms[0].appendChild(para);
var description = document.createElement("p");
description.setAttribute("id","description");
var txt1 = document.createTextNode("This is picture");
description.appendChild(txt1);
//Boms[0].appendChild(description);
var gallery = document.getElementById("imagegallery");
//gallery.parentNode.insertBefore(para,gallery);
//gallery.parentNode.insertBefore(description,gallery);
var gallery = document.getElementById("imagegallery");
gallery.insertAfter(place,gallery);
gallery.insertAfter(description.gallery);
}
function insertAfter(newElement,targetElement)
{
var parend = targetElement.parentNode
if(parend.lastChild == targetElement)
{
parend.appendChild(newElement);
}
else{
parend.insertBefore(newElement,parend.nextSibling);
}
}
//------------------------------------------------------------------------
function eventcoolter(fun)
{
var onrired = window.onload;
if(typeof window.onload != 'function'){
window.onload = fun;
}
else{
onrired();
fun();
}
}
//----- ----------------------------------------------------------------------
eventcoolter(preparePlaceholder())
通过document.createElement("img")创建的元素节点中的属性节点无法被获取 IE报错place未定义 可是img的id已经通过para.setAttribute("id","place");进行创建了为什么获取不到 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询