The attributes and styles of jquery. attr () and. Removeattr ()

Source: Internet
Author: User

The attr () method is used in query to get and set element properties, attr is an abbreviation for attribute (attribute), which is often used in jquery DOM operations attr ()

attr () has 4 expressions

    1. attr (incoming property name): Gets the value of the property
    2. Attr (property name, property value): Sets the value of the property
    3. Attr (property name, function value): Sets the function value of the property
    4. attr (attributes): Sets multiple attribute values for the specified element: {attribute name one: attribute value one, property name two: ' Property value two ', ...}

Removeattr () Delete method

. Removeattr (AttributeName): Removes an attribute (attribute) for each element in the matching element collection

Advantages:

attr and Removeattr are jquery. For attribute manipulation encapsulation, call the method directly on a JQuery object, it is easy to manipulate the property, and do not need to deliberately understand the browser's property name of the different problems

Note the issue:

There is a conceptual distinction in DOM: attribute and property are translated as "attributes", and "JS Advanced Programming" is translated into "attributes" and "attributes". Simply understood, attribute is the property of the DOM node's own

<!DOCTYPE HTML><HTML><Head>    <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />    <title></title>    <style>input{Display:Block;margin:10px;padding:10px;background:#bbffaa;Border:1px solid #ccc;    }    </style>    <Scriptsrc= "Http://libs.baidu.com/jquery/1.9.1/jquery.js"></Script></Head><Body>    <H2>. attr () and. Removeattr ()</H2>    <H3>. attr</H3>    <form>        <inputtype= "text"value= "Set Value" />        <inputtype= "text"value= "Get Value"/>        <inputtype= "text"value= "Callback Stitching value" />        <inputtype= "text"value= "Delete Value" />    </form>    <Scripttype= "Text/javascript">        //Locate the first input, set the value of the property value by attr        $("Input:first"). attr ('value','. attr (AttributeName, value)')    </Script>    <Scripttype= "Text/javascript">        //find the second input, get the value of the property value by attr        $("Input:eq (1)"). attr ('value')    </Script>    <Scripttype= "Text/javascript">        //find a third input by using a function to set the property        //You can return the final desired property value based on other property values on the element        //For example, we can associate a new value with an existing value:        $("Input:eq (2)"). attr ('value',function(I, Val) {return 'through function Settings' +val}) </Script>    <Scripttype= "Text/javascript">        //find fourth input by using removeattr to delete attributes        $("Input:eq (3)"). Removeattr ('value')    </Script></Body></HTML>

The attributes and styles of jquery. attr () and. Removeattr ()

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.