您的位置:首页 > Web前端 > JavaScript

js笔记4内置对象

2016-07-17 13:28 471 查看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p id="pid" name="pn">hello</p>

<p name="pn">hello</p>

<p name="pn">hello</p>

<p name="pn">hello</p>
<a id="aid" title="得到了a标签的属性">hellooooo
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<div id="div">
<p id="pid2">div的p元素</p>
</div>

</a>
<script>

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

function getAttr(){
var anode=document.getElementById("aid");
var attr=anode.getAttribute("title");
alert(attr);
}
getAttr();

function setAttr(){
var anode=document.getElementById("aid");
anode.setAttribute("title","titleeeee动态设置");
var attr=anode.getAttribute("title");
alert(attr);
}
setAttr();

function getChildNode(){
var childnode =document.getElementsByTagName("ul")[0].childNodes;
alert(childnode.length);
alert(childnode[2].nodeType);
}
getChildNode();
function  getParentNode(){
var div=document.getElementById("pid");
alert(div.parentNode.nodeName);

}
getParentNode();

function createNode(){
var body=document.body;
var input=document.createElement("input");
input.type="button";
input.value="按钮"
body.appendChild(input);

}
createNode();

function addNode(){
var div=document.getElementById("div");
var node=document.getElementById("pid2");
var newnode=document.createElement("p");
newnode.innerHTML="动态添加第一个元素";
div.insertBefore(newnode,node);

}
addNode();

function removeNode(){
var div=document.getElementById("div");
var p=div.removeChild(div.childNodes[1]);

}
removeNode();
function getSize() {
var width = document.body.offsetWidth||document.documentElement.offsetWidth;
var height = document.documentElement.offsetHeight;
alert("width" + width + "height" + height);
}
getSize();

</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: