document对象
<!DOCTYPE html>
<html lang="en">
<head>
<title>document对象</title>
<script>
function f1(){
var d1=document.getElementById("d1");
var d2=document.getElementById("d2");
d1.innerText=
"<h3 style='color:red'>今天是星期三</h3>";
d2.innerHTML=
"<h3 style='color:red'>3月9日</h3>";
}
function f2(){
var a1=document.getElementById("a1");
a1.removeAttribute("href");
}
function f3(){
var p1=document.getElementById("p1");
p1.style.color="blue";
p1.style.fontSize="50px";
p1.style.fontFamily="楷体";
p1.style.textAlign="center";
p1.style.textDecoration="underline";
}
var num=30;
function btnBig(){
var p1=document.getElementById("p1");
num=num+10;
p1.style.fontSize=num+"px";
}
function btnSmall(){
var p1=document.getElementById("p1");
num=num-10;
p1.style.fontSize=num+"px";
}
function btnHid(){
var g1=document.getElementById("g1");
g1.style.visibility="hidden";
}
function btnShow(){
var g1=document.getElementById("g1");
g1.style.visibility="visible";
}
function btnChange(){
var g1=document.getElementById("g1");
g1.setAttribute("src","n.jpg");
}
</script>
<style>
#p1{
color:red;
font-size:30px;
}
</style>
</head>
<body>
<div id="d1"><em>hello</em></div>
<div id="d2"><em>你好</em></div>
<a id="a1" href="http://www.baidu.com">
点我去百度</a>
<br/>
<img id="g1" src="m.jpg" width="300" height="200"/>
<br/>
<p id="p1">我是一个段落标记</p>
<input type="button" value="点我"
onclick="f3();" id="btn" />
<input type="button" value="变大"
onclick="btnBig();"/>
<input type="button" value="变小"
onclick="btnSmall();"/>
<br/>
<input type="button" value="显示"
onclick="btnShow();"/>
<input type="button" value="隐藏" onclick="btnHid();"/>
<input type="button" value="切换"
onclick="btnChange();"/>
</body>
</html>
通过name进行查找
<!DOCTYPE html>
<html lang="en">
<head>
<title>通过name进行查找</title>
<script>
function f1(){
var nodes=
document.getElementsByName("sex");
alert(nodes.length);
alert(nodes[0].value);
alert(nodes[1].value);
}
</script>
</head>
<body>
<!--关于性别的单选按钮-->
请选择性别:
男<input type="radio" name="sex" value="man"/>
女<input type="radio" name="sex" value="woman"/>
<input type="button" value="确定" onclick="f1();"/>
</body>
</html>
标签名查找
<!DOCTYPE html>
<html lang="en">
<head>
<title>标签名查找</title>
<script>
function f1(){
var pNodes=
document.getElementsByTagName("p");
var p1=pNodes[0];
p1.style.color="red";
p1.innerHTML="今天是星期五";
var d1=document.getElementById("d1");
var pp=d1.getElementsByTagName("p");
alert(pp.length);
alert(pp[0].innerHTML);
alert(pp[1].innerHTML);
}
</script>
</head>
<body>
<p>段落1</p>
<div id="d1">
<p>div中的段落1</p>
<p>div中的段落2</p>
</div>
<input type="button" value="确定" onclick="f1();"/>
</body>
</html>
层次关系查询节点
<!DOCTYPE html>
<html lang="en">
<head>
<title>层次关系查询节点</title>
<script>
function f1(){
var select1=
document.getElementById("select1");
var cArr=select1.childNodes;
for(var i=1;i<cArr.length;i=i+2){
}
var d1=document.getElementById("d1");
var d2=document.getElementById("d2");
var a1=document.getElementById("a1");
a1.innerHTML="去淘宝啊";
a1.href="http://www.taobao.com";
alert(d1.parentNode.getAttribute("id"));
alert(select1.getAttribute("id"));
}
</script>
</head>
<body>
<p>段落文本</p>
<select id="select1">
<option value="1" id="d1">a</option>
<option value="2" id="d2">b</option>
<option value="3" id="d3">c</option>
</select>
<a href="http://www.baidu.com" id="a1">ClickMe</a>
<input type="button" value="确定"
onclick="f1();"/>
</body>
</html>
删除元素节点
<!DOCTYPE html>
<html lang="en">
<head>
<title>删除元素节点</title>
<script>
function f1(){
var fm=document.getElementById("fm");
var dv=document.getElementById("dv");
var link=
document.getElementById("link");
link.parentNode.removeChild(link);
}
</script>
</head>
<body>
<form id="fm">
<input type="button" value="删除节点" onclick="f1();" id="btn"/>
<div id="dv" style="border:1px solid black;width:100px;height=70px;">
<a href="" id="link">点我去百度</a>
<p>我是一个段落标记</p>
</div>
</form>
</body>
</html>
|