Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to query attribute values by jquery

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to query attribute values in jquery". In daily operation, I believe many people have doubts about how to query attribute values in jquery. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to query attribute values in jquery". Next, please follow the editor to study!

Jquery query attribute value method: 1, use attr (), you can return the selected element attribute value, syntax "$(selector) .attr (" attribute name ")"; 2, use prop (), you can return the first matching element attribute value, syntax "$(selector) .prop (" attribute name ")".

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

Attributes can provide some additional information for the HTML tag, or decorate the HTML tag. The attribute needs to be added to the opening tag, and the syntax format is:

Attr= "value"

Attr represents the property name, and value represents the attribute value. Property values must be surrounded by double quotes "" or single quotation marks''.

Note that although both double quotes and single quotes can surround attribute values, use double quotes as much as possible for the sake of standardization and professionalism.

A tag can have no attributes, or it can have one or more attributes.

Example:

So how do you query attribute values using jquery?

In jquery, you can query attribute values in two ways

Attr ()

Prop ()

Both methods can get or set the HTML attribute of an element

1. Use the attr () method to query the attribute value

The attr () method can return the attribute value of the selected element

Syntax:

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

Example: get the value of the img src attribute, that is, the image address

$(document) .ready (function () {$("button") .click (function () {var img=$ ("img"). Attr ("src"); alert ("src attribute value is:" + img)}) })

Get the img src attribute value (picture address)

2. Use the prop () method to query the attribute value

The prop () method returns the attribute value of the selected element, which returns the attribute value of the first matching element.

Syntax:

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

Example: get the img width attribute value, that is, the width of the picture

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

Get the value of the img width attribute

At this point, the study on "how to query attribute values in jquery" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report