Let’s look at an example first:
Method 1 uses the original JavaScript method; method 2 uses this, which is equivalent to a pointer and returns a dom object. In this case, the a tag object is returned. So this.id can get the id directly. Method 3 converts the dom object into a jQuery object, and then uses the jQuery encapsulated method attr() to get the ID of the a tag.
It can be seen that sometimes it is very convenient to use javascript with jQuery. The following focuses on summarizing how jQuery operates element attributes.
◦attr(name) Get the attribute value of the element
◦attr(properties) Set element attributes in name/value format
◦attr(key,value) Set attribute value for element
◦removeAttr(name) removes the attribute value of the element
The following examples illustrate the specific usage of each method.
//Set src for the img tag to the specified image; title to Baidu.
var v = { src: "http://www.baidu.com/img/bdlogo.gif", title: "Baidu" };
jQuery("#show").attr(v);
//Set the title of img to baidu. The difference with the above is that only one attribute can be set at a time
jQuery("#show").attr("title", "baidu");
//Remove the title attribute of img
jQuery("#show").removeAttr("title");
});
You may have discovered that the attr() method in jQuery can not only obtain the attribute value of the element, but also set the attribute value of the element. Yes, there are many similar methods in jQuery. If we summarize them now, it will be easier to use them in the future.
The methods are:
◦html() Get or set the html content of the element node
◦text() Gets or sets the text content of the element node
◦height() Get or set the height of the element
◦ width() Gets or sets the width of the element
◦ val() Get or set the value of the input box
Take html() as an example, the rest are similar:
These are some basic methods for jQuery to operate element attributes. After this summary, I believe everyone will become more proficient when using jQuery.
The following are additions from other netizens:
The methods provided in jQuery are listed below:
Manipulate element attributes: each(iterator) traverses all elements in the packaging set and calls the passed iterator function for each element. Parameter iterator is a function that is called once for each element in the matching set. The argument passed to the function is set to the index (starting from 0) of the current element in the wrapped set, which can be accessed through the function's this attribute.
Get attribute value: attr(name) gets the value assigned to the specified attribute of the first element in the packaging set. The parameter name is the name of the attribute, and the value of the attribute will be obtained. If there is no such attribute, an undefined value is returned.
$("#myImage").attr("custom") The value obtained is some value.
Set attribute value: attr(name,value) sets the passed value for the name attribute of all elements in the packaging set. name is the name of the attribute that will be set, and value specifies the value of the attribute.
This function sets the title attribute of all elements on the page to a string. A string composed of the subscript of the element in the DOM and the id attribute value of each specific element.
attr() is also a quick and easy way to set multiple attributes to all elements in a wrapper set at once. attr(attributes).
This function sets the value of all elements to an empty string, and sets the title to the string Please enter a value.