Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the concept of attr in jquery

Shulou Source: shulou.com Published: 2022-05-31 18:44:47 09月21日 Update

Today, the editor will share with you the relevant knowledge of what the concept of attr is in jquery. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

In jquery, attr full name "attribute", meaning "attribute", is an attribute operation method, can return the attribute value of the element, syntax "element .attr (" attribute name ")"; you can also set one or more attribute / value pairs for the element, syntax "element .attr ({attribute: value, attribute: value.})".

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

In jquery, attr's full name is "attribute", which means "attribute" in Chinese.

Attr () is a property manipulation method in jquery that sets or returns the properties and values of the selected element.

When the method is used to return a property value, it returns the value of the first matching element.

When this method is used to set property values, one or more attribute / value pairs are set for matching elements.

Syntax:

Returns the value of the property:

$(selector) .attr ("property name")

Set properties and values:

/ / set a single property and value: $(selector) .attr ("property", "value") / / set multiple properties and values: $(selector) .attr ({attribute: value,...})

Example 1: returns an attribute value

$(document) .ready (function () {$("button") .click (function () {alert ("picture width:" + $("img"). Attr ("width"));})

Pulpit Rock

Returns the width of the picture

Example 2: set the width property of an image

$(document) .ready (function () {$("button") .click (function () {$("img"). Attr ("width", "500");})

Pulpit Rock

Set the width property for the picture

Example 3: set multiple property and value pairs

$(document) .ready (function () {$("button") .click (function () {$("img"). Attr ({width: "img"});})

Pulpit Rock

Set width and height properties for the picture

These are all the contents of the article "what is the concept of attr in jquery". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Attribute element picture multiple method knowledge article width example grammar concept full name content meaning different very large single image most is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi OPPO Reno Shulou Information Apple