日期:2014-05-16  浏览次数:20349 次

能否在某个网页元素后面增加东西?
<div>
<div   id= "id123 "> 456 </div>
<div   id= "id789 "> abc </div>
......
......
......
<div>

能否在id123的后面(而不是后部)增加文本或者HTML元素?变成

<div>
<div   id= "id123 "> 456 </div> 这里是我添加的文本 <span> 这里是我添加的元素 </span>
<div   id= "id789 "> abc </div>
......
......
......
<div>


------解决方案--------------------
这样行不?

<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> new page </title>
<script>
function a(){
var oDiv=document.getElementById( "id123 ");
oDiv.outerHTML=oDiv.outerHTML+ "这里是我添加的文本 <span> 这里是我添加的元素 </span> ";
alert(document.body.innerHTML);
}
</script>
</head>
<body onload= "a() ">
<div>
<div id= "id123 "> 456 </div>
<div id= "id789 "> abc </div>
</div>
</body>
</html>
------解决方案--------------------
可以:

<script type= "text/javascript ">
function insd(){
var mydiv=document.getElementById( "id123 ");
myspan=document.createElement( "span ");
myspan.innerHTML= "这里是我添加的元素 ";
mydiv.parentNode.insertBefore(myspan,mydiv.nextSibling);
}

</script>
<div id= "id123 "> 456 </div> <div id= "id789 "> abc </div>
......
......
......
</div>
<input type= "button " value= "插入 " onclick= "insd() " />
------解决方案--------------------
<script language= "javascript " type= "text/javascript ">
function $()
{
var arr=new Array()
var divs=document.getElementById( "hjx ").getElementsByTagName( "div ");
for(var i=0;i <divs.length;i++)
{
arr[i]=divs[i];
}
for(var i in arr)
{
document.getElementById( "hjx ").appendChild(arr[i]);
var sp=document.createElement( "span ");
sp.innerHTML= "这里是我添加的元素 ";
document.getElementById( "hjx ").appendChild(sp);
}
}
</script>
<input type= "button " value= "测试 " onclick= "$() " />
<div id= "hjx ">
<div id= "id123 "> 456 </div>
<div id= "id789 "> abc </div>
<div id= "id123 "> 456 </div>
<div id= "id789 "> abc </div>
<div>

------解决方案--------------------
<script language= "javascript " type= "text/javascript ">
function $()
{

var arr=new Array()
var divs=document.getElementById( "hjx ").getElementsByTagName( "div ");
for(var i=0;i <divs.length;i++)
{
arr[i]=divs[i];
}
for(var i=0;i <arr.length-1;i++)
{
document.getElementById( "hjx ").appendChild(arr[i]);
var sp=document.createElement( "span ");
sp.innerHTML= "这里是我添加的元素 ";