V. Basic JavaScript &dom (ii)

Source: Internet
Author: User

Note Contents Map:

Elements object (Element object)

  • To manipulate the element object, first obtain the element (obtained using the corresponding method in document)
  • The method inside the element
    • GetAttribute ("Property name"): Gets the value inside the property
      <HTML><Body>        <inputtype= "text"ID= "a"class= "haha"value= "Name"></Body>    <Scripttype= "Text/javascript">       varINPUT1=document.getElementById ("a");   alert (Input1.value); //namealert (Input1.getattribute ("value")); //namealert (Input1.class); //Undefined Note: Class is not worth the reason: class is a keyword. So you can only use GetAttribute to take class values.alert (Input1.getattribute ("class")); //hahaalert (input1.id); //aalert (Input1.getattribute ("ID")); //a</Script></HTML>
    • SetAttribute ("name", "value"): Setting properties
    • Removeattribut (name): Delete attribute
      <HTML><Body>        <inputtype= "text"ID= "a"value= "Name"></Body>    <Scripttype= "Text/javascript">       varINPUT1=document.getElementById ("a"); Alert (Input1.getattribute ("class")); //NULLInput1.setattribute ("class","haha"); //set the Class property and give the value hahaalert (Input1.getattribute ("class")); //hahaInput1.removeattribute ("class"); //Delete Attribute classalert (Input1.getattribute ("class")); //NULLInput1.removeattribute ("value"); //Delete property valuealert (Input1.getattribute ("value")); //Name Note: removeattribute cannot delete value! </Script></HTML>
  • Get the sub-label below the label
    • Use attribute ChildNodes, but this property is poorly compatible
      html><Body>        <ulID= "Ulid">        <Li>Aaaa</Li>        <Li>bbbb</Li>        <Li>Cccc</Li>    </ul></Body>    <Scripttype= "Text/javascript">       varUl11=document.getElementById ("Ulid"); varLis=Ul11.childnodes; alert (lis.length); //Under ie the result is 3, under Firefox is 7</Script></HTML>
    • So, the only effective way to get a label on the face tag is to use the getElementsByTagName method. The method return value is a collection

      <HTML><Body>        <ulID= "Ulid">        <Li>Aaaa</Li>        <Li>bbbb</Li>        <Li>Cccc</Li>    </ul></Body>    <Scripttype= "Text/javascript">       varUl11=document.getElementById ("Ulid"); varLis=Ul11.getelementsbytagname ("Li"); alert (lis.length); //3</Script></HTML>

Second, Node object

V. Basic JavaScript &dom (ii)

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.