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 jquery sets attributes to elements

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

Share

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

Editor to share with you jquery how to set properties for elements, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Setting method: 1, use attr () method, syntax "$(selector) .attr (property name, value)" or "$(selector) .attr ({property name: value;})"; 2, use prop () method, syntax "$(selector) .prop (property name, value)".

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

Jquery sets attributes to the element

1. Use the attr () method

The attr () method sets or returns the property value of the selected element. The working mode of the method is also different according to the different parameters of the method.

Syntax:

/ / single attribute $(selector) .attr (attribute name, value) / / multiple attributes $(selector) .attr ({attribute name: value; attribute value: value.})

2. Use the prop () method

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

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

Syntax:

/ / single attribute $(selector) .prop (attribute name, value) / / multiple attributes $(selector) .prop ({attribute name: value; attribute value: value.})

3. The difference between attr () and prop () methods

The prop () method is similar to the attr () method in that it is used to get or set the HTML property of an element, but there is a fundamental difference between the two.

JQuery officially recommends that attributes with values of true and false, such as checked, selected and disabled, be operated using the prop () method, while other attributes are recommended to use the attr () method.

Example:

(function () {$('input [type = "radio"]') .change (function () {var bool = $(this) .attr ("checked"); if (bool) {$("p"). Text ("you chose:" + $(this). Val ()) }) Apple, banana, watermelon

The preview effect is shown in figure 1.

Analysis.

$(). Change (function () {. })

The above is the change event in jQuery, which is the same as the onchange event in JavaScript, which we will cover in more detail later.

In this example, we actually want to determine whether the checkbox is selected by $(this) .attr ("checked"), and if so, get the value value of the checkbox. But after running the code, I found that it had no effect at all! Why is that?

In fact, the checked, selected, and disabled attributes of a form element cannot be obtained using the attr () method, but must be obtained using the prop () method. Therefore, it works by replacing the attr () method with the prop () method.

In fact, the emergence of the prop () method is to make up for the deficiency of the attr () method in the operation of form properties. Remember: if a property cannot be obtained or set using the attr () method, it can be achieved by changing the prop () method.

These are all the contents of the article "how to set attributes for elements by jquery". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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