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>