The dynamic tagging implementation method in the JavaScript document _javascript tips

Source: Internet
Author: User

The document's advanced article provides functions for node operations, including: getting nodes, changing nodes, deleting nodes, replacing nodes, creating nodes, adding nodes, cloning nodes, and so on. We can use these functions to dynamically change the nodes of the HTML.

1. JavaScript

<script type= "Text/javascript" > Function test1 () {//The case of a node ID at the same time var myhref = document.getElementById (' same ');
Window.alert (Myhref.innertext); The value of the function test2 () {//Output node is var hobbies = document.getelementsbyname ("hobby"); for (var i = 0; i < hobbies.length; i+

+) {if (hobbies[i].checked) {window.alert ("Your hobby is:" + Hobbies[i].value);}} function Getn () {//tags to get label corresponding value var myobj = document.getelementsbytagname (' input '); for (var i = 0; i < myobj.length i

+ +) {Window.alert (Myobj[i].value);}} function Addtags () {////Dynamically add Hyperlink node <a></a>//(1) Create element <a> var myelement = document.createelement ("a")//(2
Add the necessary marking information to the element myelement.href = "http://www.sina.com";
Myelement.innertext = "Connect to Sina";
MyElement.style.left = "200px";
MyElement.style.top = "300px";
MyElement.style.position = "absolute";
Add to Document.body document.body.appendChild (myelement);
} var i = 1; function Addinput () {//Add INPUT element var myelement = document.createelement (' input '); myelement.type = "button";
Myelement.value = "Run It";
Myelement.id= "i++";
Myelement.id = "ID1";
document.getElementById ("Div1"). AppendChild (myelement); The function Deleteinput () {//Deletes an element only if you want to know what its parent element is.
This method is not very flexible//method one//document.getelementbyid ("Div1"). RemoveChild (document.getElementById (' id1 '));
Method two document.getElementById (' Id1 '). Parentnode.removechild (document. getElementById (' id1 ')); } </script>

calls in the 2.body body

<body> <a id= "Same" href= "http://www.sohu.com" > Sohu </a> <a id= "Same" href= "http://www.baidu.com" > Baidu </a> <a id= "Same" href= "http://www.sina.com" > Sina </a> <input type= "button" value= "Submit" Onclick= "test1 ()"/> <!--ID is the same when only know the first--> 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.