Dom property manipulation of jquery property operations

Source: Internet
Author: User

The DOM property operation is to read, set, and remove the properties of the DOM element, such as prop (), Removeprop ().

1. Prop () 1.1 Get return value using Prop ()

Prop () returns the value of the first matching element when it returns a property value.

1<!doctype html>234<meta charset= "UTF-8" >5<title>prop () demo</title>6<script type= "Text/javascript" src= "Jquery.js" ></script>7<script>8$(function () {9             varCLA = $ ("div"). Prop ("class"));Ten Console.log (CLA); One  A             varid = $ ("div"). Prop ("id")); - Console.log (ID); -         }) the</script> - -<body> -<div class= "Divclass" id= "divID" ></div> +</body> -1.2 Use prop () to set the property value 1.2.1 use Prop () to set a single property value
1<!doctype html>234<meta charset= "UTF-8" >5<title>removeattr () demo</title>6<script type= "Text/javascript" src= "Jquery.js" ></script>7<script>8$(function () {9$ ("div"). Prop ("Class", "Divclass"));Ten$ ("div"). Prop ("id", "divid")); One         }) A</script> - -<body> the<div></div> -</body> -

The result of the execution is:

1.2.2 Setting multiple property values using prop ()
1<!doctype html>234<meta charset= "UTF-8" >5<title>prop () demo</title>6<script type= "Text/javascript" src= "Jquery.js" ></script>7<script>8$(function () {9$ ("div"). Prop ({Ten"Class": "Divclass", One"id": "divID" A             }) -         }) -</script> the -<body> -<div></div> -</body> +

The result of the execution is:

2. Removeprop ()

return value: JQuery

Description: Deletes an attribute (property) for the matching element in the collection.

The syntax format is:

$ (selector). Remoceprop (PropertyName)

which

PropertyName

Type: string

The name of the property to remove

$ (selector). The Removeprop () method is used to delete the property value set by the $ (selector). Prop () method.

If you attempt to remove some of the built-in properties on the DOM element or the Window object, the browser may produce an error.

If you do, jquery first sets the property to undefined and ignores any browser-generated errors.

In general, you only need to remove the custom property, rather than removing the built-in (native) property.

Attention:

Do not use this method to delete native properties, such as checked, disabled, or selected, which will completely remove the property. Once removed, it cannot be added to the element again.

2.1 Code Examples
1<!doctype html>234<meta charset= "UTF-8" >5<title>removeprop () demo</title>6<script type= "Text/javascript" src= "Jquery.js" ></script>7<script>8$(function () {9             var$para = $ ("p");Ten  One$para. Prop ("Luggagecode", 1234); A$para. Append ("The Secret Luggage Code is:", String ($para. Prop ("Luggagecode")), "."); -$para. Removeprop ("Luggagecode"); -$para. Append ("Now the Secret Luggage code is:", String ($para. Prop ("Luggagecode")), "."); the         }) -</script> - -<body> +<p></p> -</body> +

The result of the execution is:

Dom property manipulation of jquery property 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.