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 get attributes by jquery

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how jquery obtains attributes". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how jquery obtains attributes" together!

jquery commonly used methods to obtain attributes are: 1, attr() method, can obtain and return the value of the specified attribute, syntax "$(selector).attr(" attribute name ")";2, prop() method, can return the attribute value of the selected element, syntax "$(selector).prop(" attribute name ")".

This tutorial operates on Windows 7, jquery version 1.10.2, Dell G3 computers.

There are two common methods for jquery to obtain attributes:

attr() method

prop() method

jquery attr() method

The attr() method sets or returns the attribute value of the selected element.

Example: Get the value of img element width attribute

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

Returns the width of the image

jquery prop() method

The prop() method sets or returns the properties and values of the selected element.

When this method is used to return attribute values, it returns the value of the first matching element.

Examples:

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

Returns the width of the image

Difference between attr() and prop() methods

The prop() method is similar to the attr() method in that it is used to get or set HTML attributes of an element, but there are essential differences between the two.

jQuery official recommendation: properties with two values of true and false, such as checked, selected and disabled, are recommended to use the prop() method to operate, while other properties are recommended to use the attr() method to operate.

Thank you for reading, the above is "jquery how to obtain attributes" content, after the study of this article, I believe we have a deeper understanding of how to obtain attributes of jquery this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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