DOM property to get, set, delete

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>DOM属性获取、设置、删除</title>
    <!-- 
        e.getAttribute("id");Get the value of id attribute of the element e, as long as the properties have been set are acquired 
        id attribute setting element e Red value, it may be used to add a new attributee.setAttribute ( "id", "red
        e.removeAttribute ( "id"); id attribute element to delete e

        Remember: The above method is only valid for the property within the write line 
            correct posture: e.setAttribute ( "style", " color: red; background: grey"); // or id / class / name etc; 
            remove elements of empathy property but e.removeAttribute ( "style"); // only valid for the inline style 
     );-> 
</ head > 
< body > 
    < div ID = "P1" class = "PP" align = left = "Center" just write attribute = "just write a value" > me what color </ div > 
    < Script > 
        var A = document.getElementById ( " p1 "
        console.log (a.id); // p1 
        console.log (A.class); // undefined; Unable to get class values directly 
        console.log (a.align); // Center 
        . console.log (A casually write attributes); // undefined; unable to get custom attributes 
        // e.getAttribute ( "") 
        console.log (a.getAttribute ( " the above mentioned id " )); // p1 
        console.log (a.getAttribute ( " class " )); // PP 
        console.log (a.getAttribute ( " just write property " )); // just write value 
        // e.setAttribute (" properties "," value ")
         a.setAttribute ( "Just write property " , " I re-set a property " ); // page: just write property = "I re-set a property" 
        a.setAttribute ( " style " , " background: Red; font-style: Italic; " ); // to set the properties of the element a background-color, the value of Red 
        // e.removeAttribute (" attribute to delete ") 
        a.removeAttribute ( " align = left " ); // delete elements of the align attribute a 
    </ Script > 
</ body > 
</ HTML >

Guess you like

Origin www.cnblogs.com/vinson-blog/p/12046217.html
Recommended