官术网_书友最值得收藏!

  • jQuery 1.4 Reference Guide
  • Jonathan Chaffer Karl Swedberg Karl Swedberg
  • 555字
  • 2021-08-20 16:29:46

General attributes

These methods get and set DOM attributes of elements.

.attr() (getter)

Note

Get the value of an attribute for the first element in the set of matched elements.

.attr(attributeName)
Parameters
  • attributeName: The name of the attribute to get
Return value

A string containing the attribute value.

Description

It's important to note that the .attr() method gets the attribute value for only the first element in the matched set. To get the value for each element individually, we need to rely on a looping construct such as jQuery's .each() method.

Using jQuery's .attr() method to get the value of an element's attribute has two main benefits:

  • Convenience: It can be called directly on a jQuery object and chained to other jQuery methods.
  • Cross-browser consistency: Some attributes have inconsistent naming from browser to browser. Furthermore, the values of some attributes are reported inconsistently across browsers, and even across versions of a single browser. The .attr() method reduces such inconsistencies.

.attr() (setter)

Note

Set one or more attributes for the set of matched elements.

.attr(attributeName, value)
.attr(map)
.attr(attributeName, function)
Parameters (first version)
  • attributeName: The name of the attribute to set
  • value: A value to set for the attribute
Parameters (second version)
  • map: A map of attribute-value pairs to set
Parameters (third version)
  • attributeName: The name of the attribute to set
  • function: A function returning the value to set
Return value

The jQuery object, for chaining purposes.

Description

The .attr() method is a convenient and powerful way to set the value of attributes, especially when setting multiple attributes or using values returned by a function. Let's consider the following image:

<img id="greatphoto" src="brush-seller.jpg"
  alt="brush seller" />
Setting a simple attribute

We can change the alt attribute by simply passing the name of the attribute and its new value to the .attr() method.

$('#greatphoto').attr('alt', 'Beijing Brush Seller');

We can add an attribute the same way.

$('#greatphoto')
  .attr('title', 'Photo by Kelly Clark');
Setting several attributes at once

To change the alt attribute and add the title attribute at the same time, we can pass both sets of names and values into the method at once using a map (JavaScript object literal). Each key-value pair in the map adds or modifies an attribute:

$('#greatphoto').attr({
  alt: 'Beijing Brush Seller',
  title: 'photo by Kelly Clark'
});

When setting multiple attributes, the quotation marks around attribute names are optional.

Computed attribute values

By using a function to set attributes, we can compute the value based on other properties of the element. For example, we could concatenate a new value with an existing value as follows:

$('#greatphoto').attr('title', function() {
  return this.alt + ' – photo by Kelly Clark'
});

This use of a function to compute attribute values can be particularly useful when we modify the attributes of multiple elements at once.

.removeAttr()

Note

Remove an attribute from each element in the set of matched elements.

.removeAttr(attributeName)
.removeAttr(function)
Parameters (first version)
  • attributeName: An attribute to remove
Parameters (second version)
  • function: A function returning the attribute to remove
Return value

The jQuery object, for chaining purposes.

Description

The .removeAttr() method uses the JavaScript removeAttribute() function, but it has the advantage of being able to be called directly on a jQuery object and it accounts for different attribute naming across browsers.

As of jQuery 1.4, the .removeAttr() function allows us to indicate the attribute to be removed by passing in a function.

主站蜘蛛池模板: 红原县| 霍山县| 汨罗市| 明光市| 乐都县| 南召县| 辽阳县| 南澳县| 彭阳县| 米泉市| 山西省| 界首市| 黄浦区| 杨浦区| 四子王旗| 石景山区| 天长市| 杭锦后旗| 绥芬河市| 德庆县| 浠水县| 定州市| 桂平市| 济宁市| 洛南县| 衡东县| 定西市| 嘉祥县| 团风县| 丹棱县| 金阳县| 清新县| 乳源| 正镶白旗| 长子县| 时尚| 六枝特区| 南雄市| 东阿县| 静乐县| 张掖市|