Dom parentNode previousSibling nextSibling在FF,GG下与

分类:Javascript| 发布:camnprbubuol| 查看: | 发表时间:2010/11/25

测试代码是:

<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会忽略这些,所以最好别留空格或者换行,就行了。

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/archives/97.html