Javascript如何分层:
我们把javascript分为三层,从下往上依次是base层、common层和page层。每一层所完成的任务如下图:

Base层:
浏览器兼容问题:
1.nextSlibing.nodeType的值在火狐和IE下的值是不同的例如:
<ul>
<li id="item1"></li>
<li id="item2"></li>
<li id="item3"></li>
<ul>
<script type="text/javascript">
var item1=document.getElementById("item1");
alert(item1.nextSlibing.nodeType);
alert(document.getElementsByTagName("ul")[0].childNodes.length);
</script>
在IE下会弹出1和3在火狐下会弹出3和7,原因就是火狐会将包括空白和换行等文本信息也当做childNodes的一员,而IE则会忽略它。
所以了解决火狐和IE的兼容性问题,就封装了一个获取当前节点下一个兄弟结点的函数:
<script type="text/javascript"> function getNextNode(node){ node = typeof node = "String"?document.getElementById(node):node; var nextnode = node.nextSibling; if(!nextnode) return null; if(document.all){ while(true){ if(nextNode.nodeType==1){ break; }else{ if(nextNode.nexrSibling){ nextNode=nextNode.nextSibling; }else{ break; } } } } return nextnode; } </script>

本文深入探讨了JavaScript的分层概念,着重解析Base层如何解决浏览器兼容性问题,通过实例展示了如何封装函数以适配不同浏览器环境。

1110

被折叠的 条评论
为什么被折叠?



