Home > Web Front-end > JS Tutorial > body text

Learn Jquery with JQuery API Part 2 Properties_jquery

WBOY
Release: 2016-05-16 18:29:52
Original
1112 people have browsed it

We often need to change node attributes, content, and text, so we need to use several functions here.

1 attribute
Here we see an attr() function, which has four forms. Attr(name) is to get a selected attribute value, attr(properties)attr (key, value) attr (key, fn) is to assign a value to an attribute
What I want to say here is that there is such a design in Jquery. The same function is used to obtain the value of an object and assign a value to an object. Use fn(name) to assign a value to an object, and use fn(name, value) to assign a value to an object.
First look at how attr(name) gets an attribute value
We have
Learn Jquery with JQuery API Part 2 Properties_jquery
In this way we hope to get the value of stc in img. First we use the selector to select this img $ ("img") gets the node above, and then we use attr() to get the value of src: $("img").attr("src")
In this way we get the value of 1.jpg

Now we want to replace the image 1.jpg with the image 2.jpg: just use $("img").attr("src","2.jpg"), this is 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, which is actually the same as the first one
The third form, that is, attr (properties) is in josn format. Name: value is used to assign attributes. In Jquery, this method is very commonly used, and the structure is also very clear
For example, if we want to set the alt and src of img, we can directly write it as $("img").attr( {src:”4.jpg”,alt:”Description”})
Generally when we build a system, we need to add, modify and delete. We have talked about the first two before, so how to delete an attribute?
Here we need to use removeAttr(name), which is easy to understand. It is just the opposite of arrt(name). One is to get an attribute, and the other is to delete it. An attribute $("img").removeAttr("alt")

Okay, let's summarize several functions for operating attributes
Search: $("img").attr("src") Find the value of src
Add/modify $(“img”).attr(“src”,”2.jpg”) $(“img”).attr(“src”,function(){return “2 .jpg"}) $("img").attr({src:"2.jpg"})
Delete $("img").removeAttr("src")
2 css class
When we need to add a css style to a node such as


, what do we need to do?
According to the above attribute method, we first add a css Write a class style .sty{...}, we select this div and add a class $("div").attr("class","sty"),
Later we found out if this is too It's troublesome, so I invented addClass(class) to abbreviate the above
$("div").addClass("sty") and that's it.
Since it has been added, of course it is inevitable to delete $("div").removeClass("sty") to delete the class sty

There is also a function toggleClass (class), if it exists, delete it, if it does not exist, delete it Just add.
This is indeed a very weird function. What is the use of it? For example, we are in a group of one





If we add this function to both mouse entry and mouse movement out of li, we can achieve the effect of mouse movement

3 html
Here we see a html() and an html(val), which is the type of function we mentioned earlier: fn(name) fn(name, value)
Html() is Get the html class content of the first matching element
Html (val) is to set the html class content of the matching node

4 text
There are also two functions here text() text(val) one is to get the value and the other is to set the value. It is similar to html
The difference in the value is that html() takes the html class content of the first element that matches, and text is the obtained text. If the html node
is ignored and assigned a value, text() will convert the html class content such as into text and html() will still be interpreted as the html element

5 value
Here are still the same val() and val(val), one is to get the value, and the other is to set the value. For example,

We get this value some text $("input").val ()
Set this value $("input").val("xxiu")


Here we have finished looking at the attributes and found that we only looked at 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]) gets/sets a text
val([val]) gets/sets a value
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!