javascript的document中的動態(tài)添加標簽實現(xiàn)方法
document的高級篇中提供了節(jié)點操作的函數(shù),具體包括:獲取節(jié)點,改變節(jié)點,刪除節(jié)點,替換節(jié)點,創(chuàng)建節(jié)點,添加節(jié)點,克隆節(jié)點等函數(shù)。我們可以利用這些函數(shù)動態(tài)改變html的節(jié)點。
1、JavaScript
<script type="text/javascript"> function test1(){//對個節(jié)點的ID相同時候的情況 var myhref = document.getElementById('same'); window.alert(myhref.innerText); } function test2() {//輸出節(jié)點的值 var hobbies = document.getElementsByName("hobby"); for (var i = 0; i < hobbies.length; i++) { if (hobbies[i].checked) { window.alert("你的愛好是:" + hobbies[i].value); } } } function getN() {//通過標簽獲取標簽對應(yīng)的值 var myObj = document.getElementsByTagName('input'); for (var i = 0; i < myObj.length; i++) { window.alert(myObj[i].value); } } function addtags() {//動態(tài)添加超鏈接節(jié)點<a></a> //(1)創(chuàng)建元素<a> var myElement = document.createElement("a") //(2)給元素添加必要的標示信息 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() { //刪除一個元素的前提是要知道其父元素是什么。此方法不是很靈活 //方法一 //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相同的時候只認識第一個 --> <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>獲取指定標簽的內(nèi)容</h1> <input type="button" value="智能獲取" onclick="getN()"> --> <hr/> <h1>智能添加標簽</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ī)淼膉avascript的document中的動態(tài)添加標簽實現(xiàn)方法全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
淺析使用BootStrap TreeView插件實現(xiàn)靈活配置快遞模板
這篇文章主要介紹了使用bootstrap-treeview插件實現(xiàn)靈活配置快遞模板的相關(guān)資料,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2016-11-11使用JS判斷是否數(shù)字和小數(shù)點組合的數(shù)字的兩中方法比較(isNaN和逐判斷)
使用js判斷數(shù)字和小數(shù)點的方法非常之多。但是就目前而言,我見過最好用的判斷方法應(yīng)該來說是isNaN,它比較方便,而逐個比較的方法有一定的弊端。2009-09-09微信小程序提取公用函數(shù)到util.js及使用方法示例
這篇文章主要介紹了微信小程序提取公用函數(shù)到util.js及使用方法,結(jié)合實例形式分析了util.js公用函數(shù)相關(guān)調(diào)用操作技巧,需要的朋友可以參考下2019-01-01JavaScript數(shù)組去重的方法總結(jié)【12種方法,號稱史上最全】
這篇文章主要介紹了JavaScript數(shù)組去重的方法,結(jié)合實例形式較為詳細的總結(jié)分析了12種方法數(shù)組去重的方法,需要的朋友可以參考下2019-02-02