DOM的简单使用

 访问指定节点:

getElementsByName():

 

 

 

 

<html> <head> <title>DOM技术</title> </head> <body> <form method="post" action="document.cgi"> <fieldset> <legend>选择你喜欢的颜色!</legend> <input type="radio" name="color" value="red"/>red<br/> <input type="radio" name="color" value="green"/>green<br/> <input type="radio" name="color" value="blue"/>blue<br/> </fieldset> <input type="submit" value="submit"> </form> <script language="javascript"> var oRadios=document.getElementsByName("color"); alert(oRadios[0].getAttribute("value")); </script> </body> </html>

 

getElementById():

<html>
<head>
<title>
</title>
<script type="text/javascript">
function getValue(){
	var odiv1=document.getElementById("div1");
	odiv1.innerText="hello!";
}
</script>
</head>
<body οnlοad="getValue()">
<div id="div1"></div>
</body>
</html>
createElement():
<html>
<head>
<title>创建节点</title>
</head>
<body οnlοad="createM()">
</body>
</html>
<script language="javascript">
function createM(){
var op=document.createElement("p"); 
var otext=document.createTextNode("你好!");   
op.appendChild(otext); 
document.body.appendChild(op); 
}
</script>

 

removeChild():

<html>
<head>
<title>删除节点</title>
<script language="javascript">
function removeM(){
var op=document.body.getElementsByTagName("p")[0];
document.body.removeChild(op);
}
</script>
</head>
<body οnlοad="removeM()">
<p>你好!</p>
<p>hello world!</p>
</body>
</html>

 

replaceChild():

<html>
<head>
<title>替换节点</title>
<script language="javascript">
function appendM(){
var newP=document.createElement("p");
var newText=document.createTextNode("hello sansan!");
newP.appendChild(newText);
document.body.appendChild(newP);
}
</script>
</head>
<body οnlοad="appendM()">
<p>你好!</p>
<p>hello world!</p>
</body>
</html>

 

insertBefore():

<html>
<head>
<title>新消息出现在旧消息之前</title>
<script language="javascript">
function appendM(){
var newP=document.createElement("p");
var newText=document.createTextNode("hello sansan!");
newP.appendChild(newText);
var oldP=document.getElementsByTagName("p")[0];
document.body.insertBefore(newP,oldP);
}
</script>
</head>
<body οnlοad="appendM()">
<p>你好!</p>
<p>hello world!</p>
</body>
</html>

 

createDocumentFragment():

原方法:

<html>
<head>
<title>原方法</title>
<script language="javascript">
function oldM(){
var arrText=["first","second","third","fourth","fifth",
"sixth","seventh","eighth","ninth","tenth"];
for(var i=0;i<arrText.length;i++){
var op=document.createElement("p");
var otext=document.createTextNode(arrText[i]);
op.appendChild(otext);
document.body.appendChild(op);
}
}
</script>
</head>
<body οnlοad="oldM()">
</body>
</html>

 

现方法:

<html>
<head>
<title>原方法</title>
<script language="javascript">
function oldM(){
var arrText=["first","second","third","fourth","fifth",
"sixth","seventh","eighth","ninth","tenth"];
Var oFragment=document.createDocumentfragment()//创建文档碎片
for(var i=0;i<arrText.length;i++){
var op=document.createElement("p");
var otext=document.createTextNode(arrText[i]);
op.appendChild(otext);
oFragment.appendChild(op)
}
document.body.appendChild(oFragment);
}
</script>
</head>
<body οnlοad="oldM()">
</body>
</html>

 

innerText/innerHTML:

<html>
<head>
<title>
</title>
<script type="text/javascript">
function getBackgroundColor(){
var odiv1=document.getElementById("div1");
//odiv1.innerText="<h1>new word </h1>";

odiv1.innerHTML="<h1>new word </h1>";
}
</script>
</head>
<body>
<div id="div1"></div>
<input type="button" value="getValue" onClick="getBackgroundColor()">
</body>
</html>

div相当于一个容器,通过innerText或innerHTML向其中嵌入网页内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值