欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript的document中的動(dòng)態(tài)添加標(biāo)簽實(shí)現(xiàn)方法

 更新時(shí)間:2016年10月24日 09:26:52   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇淺談javascript的document中的動(dòng)態(tài)添加標(biāo)簽實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

document的高級(jí)篇中提供了節(jié)點(diǎn)操作的函數(shù),具體包括:獲取節(jié)點(diǎn),改變節(jié)點(diǎn),刪除節(jié)點(diǎn),替換節(jié)點(diǎn),創(chuàng)建節(jié)點(diǎn),添加節(jié)點(diǎn),克隆節(jié)點(diǎn)等函數(shù)。我們可以利用這些函數(shù)動(dòng)態(tài)改變html的節(jié)點(diǎn)。

1、JavaScript

<script type="text/javascript">
function test1(){//對(duì)個(gè)節(jié)點(diǎn)的ID相同時(shí)候的情況
var myhref = document.getElementById('same');
window.alert(myhref.innerText);
}

function test2() {//輸出節(jié)點(diǎn)的值
var hobbies = document.getElementsByName("hobby");
for (var i = 0; i < hobbies.length; i++) {
if (hobbies[i].checked) {
window.alert("你的愛(ài)好是:" + hobbies[i].value);
}
}
}

function getN() {//通過(guò)標(biāo)簽獲取標(biāo)簽對(duì)應(yīng)的值
var myObj = document.getElementsByTagName('input');
for (var i = 0; i < myObj.length; i++) {
window.alert(myObj[i].value);
}
}

function addtags() {//動(dòng)態(tài)添加超鏈接節(jié)點(diǎn)<a></a>
//(1)創(chuàng)建元素<a>
var myElement = document.createElement("a")
//(2)給元素添加必要的標(biāo)示信息
myElement.;
myElement.innerText = "連接到新浪";
myElement.style.left = "200px";
myElement.style.top = "300px";
myElement.style.position = "absolute";
//添加到document.body
document.body.appendChild(myElement);
}

var i = 1;
function addinput() {//添加input元素
var myElement = document.createElement('input');
myElement.type = "button";
myElement.value = "奔跑吧";
//myElement.id="i++";
myElement.id = "id1";
document.getElementById("div1").appendChild(myElement);
}

function deleteinput() {
//刪除一個(gè)元素的前提是要知道其父元素是什么。此方法不是很靈活
//方法一
//document.getElementById("div1").removeChild(document.getElementById('id1'));
//方法二
document.getElementById('id1').parentNode.removeChild(document
.getElementById('id1'));
}
</script>

2.body體中的調(diào)用

<body>
<a id="same" >搜狐</a>
<a id="same" >百度</a>
<a id="same" >新浪</a>
<input type="button" value="提交" onclick="test1()"/>
<!-- ID相同的時(shí)候只認(rèn)識(shí)第一個(gè) -->

<hr/>
<input type="checkbox" name="hobby" value="籃球"/>籃球
<input type="checkbox" name="hobby" value="足球"/>足球
<input type="checkbox" name="hobby" value="排球"/>排球
<input type="button" value="提交" name="testing" onclick="test2()"/> 

<!-- <hr/>
<h1>獲取指定標(biāo)簽的內(nèi)容</h1>
<input type="button" value="智能獲取" onclick="getN()"> -->

<hr/>
<h1>智能添加標(biāo)簽</h1>
<input type="button" value="智能添加" onclick="addtags()"/>
<hr/>
<h1>智能添加/刪除input</h1>
<div style="width:400px;height:300px;border:3px dashed red;" id="div1"></div>
<input type="button" onclick="addinput()" value="inputAdd"/>
<input type="button" onclick="deleteinput()" value="inputDelete"/>

</body>

以上就是小編為大家?guī)?lái)的javascript的document中的動(dòng)態(tài)添加標(biāo)簽實(shí)現(xiàn)方法全部?jī)?nèi)容了,希望大家多多支持腳本之家~

相關(guān)文章

最新評(píng)論