Javascript Base element Operations

Source: Internet
Author: User

First, get the element

1. Find Elements by ID

How to obtain document:

The way to get elements by ID is as follows:

12 varidq = document.getElementById("idq");alert(idq);

  

2. Get elements based on class

12 varo_class1 = document.getElementsByClassName("divclass");//(数组)alert(o_class1);

  

3. Get elements according to tags

4. Find the element according to the name

    var ss = Document.getelementsbyname ("ys")    alert (ss[0]);

Name is multiple and plural.

Ii. Elements of Operation

1. Operation Contents

① Non-form elements

(1) Get text: InnerText

(2) Set text

(3) Get HTML code

If you want to get all the code in a div, including span:

(4) Set HTML code

Set the HTML code, is can increase the dynamic effect, output when the page display effect. Conversely, if you use InnerText, the effect (<b> bold </b>) is also displayed.

② form Elements

(1) Assign value (set a value)

    var B1 = document.getElementById ("B1");//    alert (value = "Please enter content");  Prompt box output    b1.value = "Please enter content";   Web page Output

(2) Get Value (value)

alert (B.value);

2. Operation Properties

① adding properties (Settings)

Add attribute    var d1 = document.getElementById ("IDQ");    D1.setattribute ("Style", "color:red");

② Getting properties

GetAttribute

<div id= "IDQ" iid= "I'm Here" ><span> get text </span></div>
<script type= "Text/javascript" > var d1 = document.getElementById ("IDQ"); Alert (D1.getattribute ("IID"));</script>

③ Removing properties

RemoveAttribute

    var D1 = document.getElementById ("IDQ");    D1.removeattribute ("IID");

3. Operation Style

① get Style (only inline styles are available)

JS can only get inline when it gets the style! Others like inline and external need to use jquery.

    function Showa () {        //1, gets the style        var d2 = document.getElementById ("D2");        Alert (D2.style.color)    }

② Setting Styles

    function Ang () {        var d2 = document.getElementById ("D2");        Set style        D2.style.backgroundColor = "Red"    }

Javascript Base element Operations

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.