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

怎样替换两个div之间的内容?
<input type="hidden" id="headline">
xxx
<input type="hidden" id="footline">

原生js或者jquery都可以

------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function init(){
var a=document.getElementById("headline");
var b=document.getElementById("footline");
var an=a.nextSibling;
while(b!=an){
var p=an.parentNode;
p.removeChild(an);
an=a.nextSibling;
}
}
window.onload=init;
</script>
</head>

<body>
<input type="hidden" id="headline">
<div>11</div>sss
<a href="">xx</a>
<input type="hidden" id="footline">
</body>
</html>
类似这样试试
------解决方案--------------------

//firefox和chrome支持,ie6不支持
var start= document.getElementById("headline");
ff= start.nextSibling);
ff.nodeValue="abcd";

------解决方案--------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>123</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function test(){
            var headline = document.getElementById("headline");
            var txt = headline.nextSibling;
            alert("原文本为:" + txt.nodeValue);
            txt.nodeValue = " oh, my god! ";
            alert("替换后的文本为:" + txt.nodeValue);
        }
    </script>
</head>
<body>
    <div id="divContainer">
        <input type="text" id="headline" value ="headline" />
        xxx
        <input type="text" id="footline" value ="footline" />
    </div>
    <input type="button" value="test" onclick="test()" />
</body>
</html>