测试代码是:
<h2 class="subTitle kb_tabs_toggle_open" id="tocHeadRef">
<div class="kb_tabs_toggle kb_tabs_toggle_open"></div>
<span><a href="javascript:loadTOCNode(’mysubtitle’);" id="mysubtitle">案例</a></span><div class="sectionpreview_closed">
网站建设,郑州网建
</div>
</h2>
<div style="margin-left:195px;" class="sbody kb_tabs_toggle_open">
网站建设,郑州网建,简介
</div>
<script type="text/javascript">
function loadTOCNode(obj) {
var myobj = document.getElementById(obj);
var parentClass = myobj.parentNode.parentNode.className;
var previousClass = myobj.parentNode.previousSibling.className;
alert(myobj.parentNode.previousSibling.nodeName);
if (previousClass.indexOf("kb_tabs_toggle_open") > 0) {
myobj.parentNode.parentNode.className = parentClass.replace(/kb_tabs_toggle_open/, "");
myobj.parentNode.parentNode.className += " kb_tabs_toggle_closed";
myobj.parentNode.previousSibling.className = previousClass.replace(/kb_tabs_toggle_open/, "");
myobj.parentNode.previousSibling.className += " kb_tabs_toggle_closed";
myobj.parentNode.nextSibling.style.display = "block";
myobj.parentNode.parentNode.nextSibling.style.display = "none";
} else {
myobj.parentNode.parentNode.className = parentClass.replace(/kb_tabs_toggle_closed/, "");
myobj.parentNode.parentNode.className += " kb_tabs_toggle_open";
myobj.parentNode.previousSibling.className = previousClass.replace(/kb_tabs_toggle_closed/, "");
myobj.parentNode.previousSibling.className += " kb_tabs_toggle_open";
myobj.parentNode.nextSibling.style.display = "none";
myobj.parentNode.parentNode.nextSibling.style.display = "block";
}
}
</script>
js测试:alert(myobj.parentNode.previousSibling.nodeName);
弹出结果
IE下:DIV [object HTMLDivElement] (alert(myobj.parentNode.previousSibling);)
火狐和google下:#text [object Text](alert(myobj.parentNode.previousSibling);)
alert(myobj.parentNode.nextSibling.nodeName);
弹出结果
IE下:DIV [object HTMLDivElement] (alert(myobj.parentNode.previousSibling);)
火狐和google下:DIV [object HTMLDivElement](alert(myobj.parentNode.previousSibling);)
为什么这样,因为火狐和Google浏览器会把空行,空格都当成元素,而IE会忽略这些,所以最好别留空格或者换行,就行了。