Home >Web Front-end >Front-end Q&A >jquery sets attributes to elements
As a popular JavaScript library, jQuery has the ability to conveniently and efficiently operate DOM elements. Setting attributes for elements is also one of its commonly used functions. In this article, we will discuss several commonly used property setting methods and illustrate them with specific examples.
Use attr()
method to set attributes and values for elements. For example, the following code sets the id
attribute for a dc6dce4a544fdca2df29d5ac0ea9906b
element with the value myDiv
.
$('div').attr('id', 'myDiv');
Use the attr()
method to set multiple attributes and values at the same time, for example:
$('div').attr({ id: 'myDiv', class: 'box' });
## The #prop() method is similar to the
attr() method and can set the attributes and values of the element, but there are some subtle differences between them.
prop() is mainly used to set the attributes of the element itself, such as
checked,
selected,
disabled, etc., while
attr( ) is more suitable for setting non-standard attributes of elements. For example, the following code sets an input element to the selected state:
$('input[type="checkbox"]').prop('checked', true);3. Use the data method
data() method to set data attributes for an element. Its syntax is very simple, you only need to pass the attribute to be set and the corresponding value as parameters. For example, the following code sets the
data-value attribute of a
dc6dce4a544fdca2df29d5ac0ea9906b element to
100:
$('div').data('value', 100);4. Using attr and data methods to set custom attributesUse the
attr() and
data() methods to also set custom attributes, which end with
data- starts. For example, the following code sets the custom attributes
data-count and
data-price for a
dc6dce4a544fdca2df29d5ac0ea9906b element:
$('div').attr('data-count', 10); $('div').data('price', 100);two , Attribute acquisition methodIn addition to setting the attributes of the element, you can also use jQuery to obtain the attribute value of the element. Here, we introduce three methods of obtaining attributes: 1. Use the attr methodUse the
attr() method to obtain the attribute value of the element. For example, the following code obtains the
id attribute value of a
dc6dce4a544fdca2df29d5ac0ea9906b element:
var idValue = $('div').attr('id'); console.log(idValue); // 输出 'myDiv'2. Use the prop methodSimilarly , you can also use the
prop() method to get the attribute value of an element. For example, the following code gets the selected status of a checkbox element:
var isChecked = $('input[type="checkbox"]').prop('checked'); console.log(isChecked); // 输出 true 或 false3. Use the data methodUse the
data() method to obtain the data attribute value of an element. For example, the following code obtains the
data-value attribute value of a
dc6dce4a544fdca2df29d5ac0ea9906b element. :
var dataValue = $('div').data('value'); console.log(dataValue); // 输出 1003. SummaryThis article introduces jQuery’s method of setting attributes for elements, including basic attribute setting methods and common attribute acquisition methods. Among them, the
attr() method is suitable for setting non-standard attributes, the
prop() method is suitable for setting the attributes of the element itself, and the
data() method is suitable for setting and Get the data attribute of the element. Proficient use of these methods can make it easier for us to manipulate DOM elements and improve development efficiency.
The above is the detailed content of jquery sets attributes to elements. For more information, please follow other related articles on the PHP Chinese website!