JavaScript DOM对象控制HTML元素详解

offsetHeight不包含滚动条 scrollHeight包含滚动条

通过name操作

function getName()
        {
            var count=document.getElementsByName("pn");
            alert(count.length);
            var p=count[0];
            p.innerHTML="world";
        }
        getName();

通过标签名操作

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<p name="pn">hello</p>
<p name="pn">hello</p>
<p name="pn">hello</p>
    <script>
        function getName()
        {
            var count=document.getElementsByTagName("p");
            alert(count.length);
            var p=count[0];
            p.innerHTML="world";
        }
        getName();
    </script>

</body>
</html>
得到属性:


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<p name="pn">hello</p>
<p name="pn">hello</p>
<p name="pn">hello</p>
<a id="aid" title="得到a标签的属性">hello</a>
    <script>
        function getAttr()
        {
            var anode =document.getElementById("aid");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getName()
        {
            var count=document.getElementsByTagName("p");
            alert(count.length);
            var p=count[0];
            p.innerHTML="world";
        }
        getAttr();
    </script>

</body>
</html>

设置属性:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<p name="pn">hello</p>
<p name="pn">hello</p>
<p name="pn">hello</p>
<a id="aid" title="得到a标签的属性">hello</a>
<a id="aid2">aid2</a>
    <script>
        function getAttr()
        {
            var anode =document.getElementById("aid");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getName()
        {
            var count=document.getElementsByTagName("p");
            alert(count.length);
            var p=count[0];
            p.innerHTML="world";
        }
        function setAttr()
        {
            var anode=document.getElementById("aid2");
            anode.setAttribute("title","setattributr");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        setAttr();
    </script>

</body>
</html>
获取子节点

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <a id="aid" title="得到a标签的属性">hello</a>
    <a id="aid2">aid2</a>
    <script>
        function getAttr()
        {
            var anode =document.getElementById("aid");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getName()
        {
            var count=document.getElementsByTagName("p");
            alert(count.length);
            var p=count[0];
            p.innerHTML="world";
        }
        function setAttr()
        {
            var anode=document.getElementById("aid2");
            anode.setAttribute("title","setattributr");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getChildNode()//经验证 JS定义函数必须用驼峰法!
        {
            var childNode=document.getElementsByTagName("ul")[0].childNodes;
            alert(childNode.length);
        }
        getChildNode();
    </script>

</body>
</html>
输出7 为什么呢?ur li后面都有空白项 删去空格输出3

获取父节点(只能有一个父节点)

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <a id="aid" title="得到a标签的属性">hello</a>
    <a id="aid2">aid2</a>
    <div>
        <p id="pid">div元素</p>
    </div>
    <script>
        function getAttr()
        {
            var anode =document.getElementById("aid");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getName()
        {
            var count=document.getElementsByTagName("p");
            alert(count.length);
            var p=count[0];
            p.innerHTML="world";
        }
        function setAttr()
        {
            var anode=document.getElementById("aid2");
            anode.setAttribute("title","setattributr");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getChildNode()//经验证 JS定义函数必须用驼峰法!
        {
            var childNode=document.getElementsByTagName("ul")[0].childNodes;
            alert(childNode[0].nodeType);
        }
        function getParentNode()
        {
            var div=document.getElementById("pid");
            alert(div.parentNode.nodeName);
        }
        getParentNode();
    </script>

</body>
</html>

增添子节点:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <a id="aid" title="得到a标签的属性">hello</a>
    <a id="aid2">aid2</a>
    <div>
        <p id="pid">div元素</p>
    </div>
    <script>
        function getAttr()
        {
            var anode =document.getElementById("aid");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getName()
        {
            var count=document.getElementsByTagName("p");
            alert(count.length);
            var p=count[0];
            p.innerHTML="world";
        }
        function setAttr()
        {
            var anode=document.getElementById("aid2");
            anode.setAttribute("title","setattributr");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getChildNode()//经验证 JS定义函数必须用驼峰法!
        {
            var childNode=document.getElementsByTagName("ul")[0].childNodes;
            alert(childNode[0].nodeType);
        }
        function getParentNode()
        {
            var div=document.getElementById("pid");
            alert(div.parentNode.nodeName);
        }
        function createNode()
        {
            var body=document.body;
            var input=document.createElement("input");
            input.type="button";
            input.value="按钮";
            body.appendChild(input);
        }
        createNode();
    </script>

</body>
</html>

选择性插入节点:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <a id="aid" title="得到a标签的属性">hello</a>
    <a id="aid2">aid2</a>
    <div id="div">
        <p id="pid">div元素</p>
    </div>
    <script>
        function getAttr()
        {
            var anode =document.getElementById("aid");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getName()
        {
            var count=document.getElementsByTagName("p");
            alert(count.length);
            var p=count[0];
            p.innerHTML="world";
        }
        function setAttr()
        {
            var anode=document.getElementById("aid2");
            anode.setAttribute("title","setattributr");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getChildNode()//经验证 JS定义函数必须用驼峰法!
        {
            var childNode=document.getElementsByTagName("ul")[0].childNodes;
            alert(childNode[0].nodeType);
        }
        function getParentNode()
        {
            var div=document.getElementById("pid");
            alert(div.parentNode.nodeName);
        }
        function createNode()
        {
            var body=document.body;
            var input=document.createElement("input");
            input.type="button";
            input.value="按钮";
            body.appendChild(input);
        }
        function addNode()
        {
            var div=document.getElementById("div");
            var node=document.getElementById("pid");
            var newNode=document.createElement("p");
            newNode.innerHTML="添加第一个元素";
            div.insertBefore(newNode,node);

        }
        addNode();
    </script>

</body>
</html>
删除节点:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <a id="aid" title="得到a标签的属性">hello</a>
    <a id="aid2">aid2</a>
    <div id="div">
        <p id="pid">div元素</p>
    </div>
    <script>
        function getAttr()
        {
            var anode =document.getElementById("aid");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getName()
        {
            var count=document.getElementsByTagName("p");
            alert(count.length);
            var p=count[0];
            p.innerHTML="world";
        }
        function setAttr()
        {
            var anode=document.getElementById("aid2");
            anode.setAttribute("title","setattributr");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getChildNode()//经验证 JS定义函数必须用驼峰法!
        {
            var childNode=document.getElementsByTagName("ul")[0].childNodes;
            alert(childNode[0].nodeType);
        }
        function getParentNode()
        {
            var div=document.getElementById("pid");
            alert(div.parentNode.nodeName);
        }
        function createNode()
        {
            var body=document.body;
            var input=document.createElement("input");
            input.type="button";
            input.value="按钮";
            body.appendChild(input);
        }
        function addNode()
        {
            var div=document.getElementById("div");
            var node=document.getElementById("pid");
            var newNode=document.createElement("p");
            newNode.innerHTML="添加第一个元素";
            div.insertBefore(newNode,node);

        }
        function removeNode()
        {
            var div=document.getElementById("div");
            var p=div.removeChild(div.childNodes[1]);
        }
        removeNode();
    </script>

</body>
</html>

尺寸:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <p name="pn">hello</p>
    <a id="aid" title="得到a标签的属性">hello</a>
    <a id="aid2">aid2</a>
    <div id="div">
        <p id="pid">div元素</p>
    </div>
    <script>
        function getAttr()
        {
            var anode =document.getElementById("aid");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getName()
        {
            var count=document.getElementsByTagName("p");
            alert(count.length);
            var p=count[0];
            p.innerHTML="world";
        }
        function setAttr()
        {
            var anode=document.getElementById("aid2");
            anode.setAttribute("title","setattributr");
            var attr=anode.getAttribute("title");
            alert(attr);
        }
        function getChildNode()//经验证 JS定义函数必须用驼峰法!
        {
            var childNode=document.getElementsByTagName("ul")[0].childNodes;
            alert(childNode[0].nodeType);
        }
        function getParentNode()
        {
            var div=document.getElementById("pid");
            alert(div.parentNode.nodeName);
        }
        function createNode()
        {
            var body=document.body;
            var input=document.createElement("input");
            input.type="button";
            input.value="按钮";
            body.appendChild(input);
        }
        function addNode()
        {
            var div=document.getElementById("div");
            var node=document.getElementById("pid");
            var newNode=document.createElement("p");
            newNode.innerHTML="添加第一个元素";
            div.insertBefore(newNode,node);

        }
        function removeNode()
        {
            var div=document.getElementById("div");
            var p=div.removeChild(div.childNodes[1]);
        }
        function getSize()
        {
            var width=document.documentElement.offsetWidth||document.body.offsetWidth;
            var height=document.documentElement.offsetHeight||document.body.offsetHeight;
            alert(width+","+height);
        }
        getSize();
    </script>

</body>
</html>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值