Re从零开始的实习生活01——jQuery中attr()和prop()的区别

    xiaoxiao2021-04-18  67

    达人科技 2017-04-08 09:45

    <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="UTF-8">

    <title>Attribute与Property的区别</title>

    </head>

    <body>

    复选框<input type="checkbox" id="testCheckBox"/>

    </body>

    </html>

    <script>

    var oCheckbox=document.getElementById("testCheckBox");

    // 设置Attribute

    oCheckbox.setAttribute("value","boxValue");

    console.log(oCheckbox.value);//boxValue

    // 设置Property

    oCheckbox.name="boxName";

    console.log(oCheckbox.getAttribute("name"));//boxName

    // 自定义Attribute

    oCheckbox.setAttribute("myValue","myBoxValue");

    console.log(oCheckbox.myValue);//undefined

    // 自定义Property

    oCheckbox.myName="myBoxName";

    console.log(oCheckbox.getAttribute("myName"));//null

    // 设置Property(number类型)

    oCheckbox.num=1;

    console.log(typeof(oCheckbox.num));//number

    // 设置Property(boolean类型)

    oCheckbox.isPost=true;

    console.log(typeof(oCheckbox.isPost));//boolean

    // attribute支持类型

    oCheckbox.setAttribute("attrValue",1);

    console.log(typeof(oCheckbox.getAttribute("attrValue")));//string

    oCheckbox.setAttribute("isChecked",true);

    console.log(typeof(oCheckbox.getAttribute("isChecked")));//string </script>

    转载请注明原文地址: https://ju.6miu.com/read-675168.html

    最新回复(0)