Home >
Article > Web Front-end > jquery gets custom attributes (attr and prop) examples introduction_jquery
jquery gets custom attributes (attr and prop) examples introduction_jquery
WBOYOriginal
2016-05-16 17:36:031678browse
$("form").attr("check"); $("form").prop("check"); Both are acceptable, but the new version of jquery recommends the second one. Both are good in other aspects. Almost the same, the only difference I found is that when using the checkbox, you need to use prop, otherwise the IE browser will be incompatible
Attachment: jquery attr() method The attr() method is used in jquery to get and set element attributes. attr is the abbreviation of attribute. attr() is often used in jQuery DOM operations. attr() has 4 expressions.
1.
attr(Attribute name) //Get the value of the attribute (get the attribute value of the first matching element. This method can easily start from the first matching element Get the value of an attribute in an element. If the element does not have a corresponding attribute, undefined is returned)
2.
attr(Attribute name, attribute value) //Set the value of the attribute (Set an attribute value for all matching elements.)
3.
attr(Attribute name,Function value) //Set the function value of the attribute (Set a calculated attribute value for all matching elements . Instead of providing a value, a function is provided, and the value calculated by this function is used as the attribute value).
4.
attr(properties) //Set multiple attribute values for the specified element, that is: {Attribute name one: "Attribute value one", Attribute name two: " Attribute value two”, … … }. (This is the best way to set many attributes in batches across all matching elements. Note that if you want to set the class attribute of an object, you must use 'className' as the attribute name. Or you can directly use 'class' or ' id'. )
It is wrong to add alt in li. It can only be used in img, area and input elements (including applet elements). For input elements, the alt attribute is intended to replace the submit button image. In order to explain the attr() method in detail here, there is no suitable attribute, so alt is used as an example. It is only for learning and reference of the attr() method usage. Here is the difference between alt and tite. alt: This is the text used to describe the graphic. When the image cannot be displayed, these texts will be displayed instead of the image. The text will also be displayed when the mouse is moved over the image. Title: It is the text that will be displayed after the mouse is placed on it.
So how to delete attributes?
The keyword for deleting attributes in jquery is: removeAttr Note that A is capitalized. See how to use it:
The same html code in usage one, I want to delete the title attribute of li, so it is like this:
It’s that simple, attr is actually a simplified implementation of getAttribute in native js, and removeAttr is the abbreviation of removeAttribute.
So is there an attribute similar to attr()? val() in jquery is similar, $(this).val(); gets the value of an element node, which is equivalent to $(this).attr("value"); $(this).val(value); Set the value of an element node, equivalent to $(this).attr("value",value);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn