HTML ():
The HTML () method returns or sets the contents of the selected element (inner HTML).
When you use this method to read multiple values , it returns the contents of the first matching element.
When you use this method to set a value, it overrides the contents of all matching elements.
When the element content is taken, the code tag below the specified element can be obtained
Text ()
Read multiple return multiple
Similar to HTML (), but only gets the plain text content of the specified element
Val ():
Returns or sets the value of the selected element ( read multiple returns only the first ), which is similar to HTML ()
Set and get by the Value property, commonly used in input elements, only on form elements
attr ():
$ (). attr (property name, property value) →$ (selector). attr (attribute,value)
You can also set multiple $ (selector). attr ({attribute:value, attribute:value ...})
Returns or sets the property value of the selected element property value
A total of four kinds of writing:
attr (property name), gets the first property value if no return undefined
Attr (property name, property value), set so that the property value of the matching element
Attr (property name, function value), setting the function value of the property
attr ({attribute name one: attribute value one, property name two: "Property value Two", ...}), bulk set property value
Prop ()
When the method is used to return a property value, the value of the first matching element is returned.
When the method is used to set property values, one or more property/value pairs are set for the matching element collection.
The four usages are the same as attr.
attr (), the use and difference of prop () :
Attribute/property |
.attr() |
.prop() |
AccessKey |
√ |
|
Align |
√ |
|
Async |
√ |
√ |
Autofocus |
√ |
√ |
Checked |
√ |
√ |
Class |
√ |
|
Contenteditable |
√ |
|
Draggable |
√ |
|
Href |
√ |
|
Id |
√ |
|
Label |
√ |
|
Location (i.e. window.location) |
√ |
√ |
Multiple |
√ |
√ |
ReadOnly |
√ |
√ |
Rel |
√ |
|
Selected |
√ |
√ |
Src |
√ |
|
TabIndex |
√ |
|
Title |
√ |
|
Type |
√ |
|
Width (if needed over .width() ) |
√ |
|
Get state representations differently:
attr (' checked '): checked (when unchecked, attr gets undefined)
Prop (' checked '): True (Unity returns true and false)
Add property name immediately in effect with prop;
There are true and false two attributes that can be used with prop;
The intrinsic attributes may be considered using prop;
Other scenarios can be considered using attr, such as custom DOM properties for action processing with attr
Value: is the HTML Tag Value property
This.value is the JS original eco-notation, $ (this). Val () is the wording of JQ
Returnreturn control to the page. return False,terminating processingReturn true to continue execution
HTML, Val, attr, prop differences. This.value and $ (this). Val () Difference and return usage