Follow jquery API to learn jquery's second attribute

Source: Internet
Author: User

We often need to change the node attributes, content, and text to use several functions.

1 attribute
Here we see an ATTR () function. There are four forms. ATTR (name) is used to obtain a selected Attribute Value. ATTR (properties) ATTR (Key, value) ATTR (Key, FN) is used to assign a value to an attribute.
Here we will talk about the design in jquery. The same function is used to obtain an object Value and assign a value to an object. FN (name) is used to assign a value to an object, assign values to an object using FN (name, value.
First, let's look at how ATTR (name) obtains a property value.
We have

In this way, we want to get the STC value in IMG. First, we use the selector to select this IMG $ ("IMG") to get the node above, and then we use ATTR () to get the SRC value: $ ("IMG "). ATTR ("src ")
In this case, we get the 1.jpg value.

now we want to replace the image 1.jpg with the image 2.jpg: Just use the image "). This is the form of FN (name, key). Of course, we know that in Javascript, for example, onclick may be a function, of course, other attributes can also be functions, so our key can also be a function $ ("IMG "). ATTR ("src", function () {return '3.jpg '}). This is the second form. In fact, it is the same as the first one.
the third form is ATTR (properties) this method is used to assign values to attributes in the format of josn. In jquery, this method is very common, the structure is also very clear
for example, if you want to set ALT and SRC of IMG, you can directly write $ ("IMG "). ATTR ({SRC: ”4.jpg ", alt:" "})
generally, the system must be added, modified, or deleted. We have mentioned the first two. How can we delete an attribute?
removeattr (name) is used here, which is the opposite of arrt (name, one is to get an attribute, and the other is to delete an attribute $ ("IMG "). removeattr ("ALT")

Now, let's summarize several functions of Operation attributes.
Search: $ ("IMG"). ATTR ("src") Find the SRC value
Add/modify the certificate (registrimg).attr(registrsrc”, choose 2.jpg ") $ (" IMG "). ATTR ("src", function () {return parts 2.jpg "}) $ (" IMG "). ATTR ({SRC: ”2.jpg "})
Delete $ ("IMG"). removeattr ("SRC ")
2. CSS class
When we need to give a node such
<Div> </div>
To add a CSS style, what should we do?
According to the method of the above attribute, we first write a class style in CSS. sty {......}, Select this Div and add a class $ ("Div"). ATTR ("class", "Sty") to it "),
Later we found that this was not too troublesome, So we invented addclass (class) to name the above.
$ ("Div"). addclass ("Sty") is done.
Since there are some additions, it is inevitable to delete the $ ("Div"). removeclass ("Sty") Delete the sty class.

Another function, toggleclass (class), is deleted if it exists. If it does not exist, it is added.
This is indeed a weird function. What is the use of this function? For example, in a group
<Ul>
<Li>
<Li>
<Li>
</Ul>
When we add this function to both the mouse entry and mouse removal over Li, we can move the mouse.

3 html
Here we see an HTML () and an HTML (VAL) are functions of the type FN (name) FN (name, value) we mentioned earlier.
HTML () is the HTML class that gets the first Matching Element.
HTML (VAL) is used to set the HTML class of matching nodes.

4 text
Here there are two functions: Text () text (VAL). One is to set the value, and the other is to set the value similar to HTML.
The difference in values is that HTML () is used to retrieve the HTML class of the First Matching Element, while text is used to retrieve all the text while ignoring the HTML node.
If the value is assigned, text () will convert HTML class content into text, for example, <A> </a>, while HTML () is still interpreted as HTML element.

5 values
Here is the same as Val () and Val (VAL). One is to get the value, and the other is to set the value, such
<Input value = "some text"/>
We get this value some text $ ("input"). Val ()
Set this value $ ("input"). Val ("xxiu ")

Here we have read the attributes. We found that we only read a few functions.
ATTR (name [, Val, | Fn]) removeattr (name) add settings to delete an attribute
Addclass (class) removeclass (class) add settings to delete a class
HTML ([Val]) GET/set a heml
Text ([Val]) GET/set a text
Val ([Val]) GET/set a value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.