首页 > 其他 > 详细

DOM基础二

时间:2014-03-13 02:59:45      阅读:438      评论:0      收藏:0      [点我收藏+]

1、获取计算后的样式:

currentStyle

  ie所支持的获取非行间样式的方法
  用法:对象.currentStyle.样式名
  例:oDiv.currentStyle.width

getComputedStyle

  非ie所支持的获取非行间样式的方法
  用法:getComputedStyle(对象,伪类).样式名
  例:getComputedStyle(oDiv,null).color

bubuko.com,布布扣
function getStyle(obj,styleName){

        if (obj.currentStyle){
            return obj.currentStyle[styleName];
        }
        else{
            return getComputedStyle(obj,null)[styleName];
        }
    }
bubuko.com,布布扣

操作节点
  createElement("标签名") : 创建新元素
  createTextNode("") : 创建文本节点

   appendChild(node) : 向childNodes末尾插入一个节点node
  insertBefore(node,targetNode) : 向targetNode之前插入节点node
  replaceChild(newNode,oldNode) : newNode替换节点oldNode
  removeChild(node) : 移除父节点的某个子节点

  getAttribute(name):获取节点上name属性的值
  setAttribute(name,value):设置节点上name属性的值为value
  removeAttribute(name):删除节点上的name属性

 

为兼容主流浏览器:

bubuko.com,布布扣
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript">
    function getStyle(obj,styleName){

        if (obj.currentStyle){
            return obj.currentStyle[styleName];
        }
        else{
            return getComputedStyle(obj,null)[styleName];
        }
    }

    function fnNext(obj){//获取下一个兄弟节点

        if (obj.nextElementSibling){
            return obj.nextElementSibling;
        }
        else{
            return obj.nextSibling;
        }
    }

    function fnPre(obj){//获取上一个兄弟节点

        if (obj.previousElementSibling){
            return obj.previousElementSibling;
        }
        else{
            return obj.previousSibling;
        }
    }

    function fnFirst(parent){//获取第一个子节点

        if (parent.firstElementChild){
            return parent.firstElementChild;
        }
        else{
            return parent.firstChild;
        }
    }

    function fnLast(parent){//获取最后一个子节点

        if (parent.lastElementChild){
            return parent.lastElementChild;
        }
        else{
            return parent.lastChild;
        }
    }

    window.onload=function (){

        var oUl=document.getElementById("ul1");
        var li2=oUl.children[1];

        // alert(fnPre(li2).innerHTML);
        // alert(li2.parentNode.tagName);
        alert(fnLast(oUl).innerHTML);

        // alert(li2.nextSibling.innerHTML);兼容ie
        // alert(li2.nextElementSibling.innerHTML);兼容现代浏览器
    }
    </script>
</head>
<body>
    <ul id="ul1">
        <li>li1</li>
        <li>li2</li>
        <li><span>li3</span></li>
        <li>li4</li>
    </ul>
</body>
</html>
bubuko.com,布布扣

DOM基础二,布布扣,bubuko.com

DOM基础二

原文:http://www.cnblogs.com/ztz13125073098/p/3596771.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!