JavaScript Dynamic Operation html

Source: Internet
Author: User
Tags tag name

< HTML >        < Body >          < P >aaaaa</p>          <type = "button" value = "Confirm" />
          <div id= "Mydiv" > some text </div>
          <input type= "Radio" value= "Red" name= "color" id= "colorred" >
          <input type= "Radio" value= "green" name= "color" id= "Colorgreen" testatr= "TestValue" >        </  Body></html>

1. Get HTML tags

var html=document.documentelement

var html=document.childnodes[0]

var html= document.firstchild

2. Get body Tag

var body= document.body

3. Get the document title

var title= document.title;

document.title= "New Titile";

4. Get Url,domain,referrer, and cross-domain issues

var url= document. url;//get the full URL information in the browser address bar

var domain= document.domain;//Get the domain name of the page

The Var referrer=document.referrer;//represents the URL address information that is connected to this page and can be empty.

For frames or inline frames that contain other subdomains in the page, you can set the referrer property to resolve cross-domain issues because JavaScript cannot be accessed across domains.

Suppose a page inside www.test.com loads an inline frame from p2p.test.com, you can set domain to test.com to solve cross-domain issues.

Specific can refer to http://www.cnblogs.com/rainman/archive/2011/02/20/1959325.html;

5. Get elements based on ID

var div= document.getelementbyid ("mydiv"); Returns only the first occurrence of the element in the document.

6. Get elements based on tag name

var img= document.getelementsbytagname ("img");//Return htmlcollection

7. Get elements based on the Name property

var radio=document.getelementsbyname ("Color");

8. Other Special Collections

Document.anchors: All <a> elements that contain the name attribute

Document.forms: All <form> elements, same as document.getElementsByTagName ("form")

Document.images: All Elements Collection

Document.links: All <a> elements with href characteristics

9. Get the custom attribute value

var q= document.getelementbyid ("mydiv"). GetAttribute ("Testatr");

10. Creating elements

var div=document.createelement ("div");

Div.id= "Newdivid";

Div.classname= "box";

Document.body.appendChild (DIV);

11. Dynamic JAVACRIPT,CSS

<script type= "Text/javascript" src= "Client.js" ></script>

or <script type= "Text/javascript" >

function Sayhi () {

Alert ("HI");

}

</script>

var script =document.createelement ("script");

Script.type= "Text/javascript";

Script.src= "Client.js" or

var function= "function Sayhi () {alert (' Hi ');}";

try{

Script.appendchild (document.createTextNode ("function"));

}catch (ex) {

script.text= "function";

}

Document.body.appendChild ("script");

Consider the different browser compatibility, summarized as follows:

<script type= "Text/javascript" >functionloadscript (URL) {//<script type= "Text/javascript" src= "Client.js" ></script>    varScript =document.createelement ("script"); Script.type= "Text/javascript"; Script.src=URL; Document.body.appendChild (script);}functionLoadscripttext (code) {//<script type= "Text/javascript" >    //function Sayhi () {Alert ("HI");}    //</script>    varScript =document.createelement ("script"); Script.type= "Text/javascript";Try{script.appendchild (document.createTextNode ("function"));Catch(ex) {//IE sees script as a special element and does not allow the script child node to be modifiedScript.text= "function"; } document.body.appendChild (script);}functionloadstyle (URL) {//<link rel= "stylesheet" type= "Text/css " href= "Style.css" >    varlink =document.createelement ("link"); Link.rel= "stylesheet"; Link.type= "Text/css"; link.href=URL; varHead =document.getelementsbytagname ("head") [0]; Head.appendchild (link);}functionLoadstyletext (CSS) {//<style type= "Text/css" >    //body{background-color:red}    //</style>    varstyle =document.createelement ("style"); Style.type= "Text/css";Try{style.appendchild (document.createTextNode (CSS)); }Catch(ex) {//IE sees style as a special element and does not allow modification of style child nodesstyle.text=CSS; }    varHead =document.getelementsbytagname ("head") [0]; Head.appendchild (link);}</script>

JavaScript Dynamic Operation html

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.