Jquery click to switch the image and hide the displayed content (two methods) _ jquery-js tutorial

Source: Internet
Author: User
The computer screen size is fixed, so how to put more content in a limited space? jquery's tab-switched browsing can solve this problem. Our computer screen size is fixed, so how to put more content in a limited space?
We should give users enough options to hide some content when they want to see it quickly. So we have the question.

In fact, this question is very simple. We share it with you. On the one hand, we can communicate with each other, and on the other hand, it is also a summary of our own learning.

I have come up with two methods to share with you.
Let's talk about the code below:

The first is the conventional method.:
[Javascript]

The Code is as follows:


$ (Function (){
Var images = ['images/up.png ', 'images/down.png']
$ (Img). onClick (function (){
If ($ (img). attr ("class") = "up "){
$ (Img). attr ("src", images [1]);
$ (Img). removeClass ();
} Else {
$ (Img). attr ("src", images [0]);
$ (Img). addClass ("up ");
}
});
})

$ (Function (){
Var images = ['images/up.png ', 'images/down.png']
$ (Img). onClick (function (){
If ($ (img). attr ("class") = "up "){
$ (Img). attr ("src", images [1]);
$ (Img). removeClass ();
} Else {
$ (Img). attr ("src", images [0]);
$ (Img). addClass ("up ");
}
});
})


Here we mainly register a click event for the image control, add CSS control when clicking (css mainly sets whether to display a part of content), and change the image at the same time.

Method 2: use arguments. callee. em ^ = 1 to automatically select array parameters
[Javascript]

The Code is as follows:


Functionchangeimg (){
// Change the image
Var images = ['images/up.png ', 'images/down.png']
Var imgupdown = document. getElementById ("hideimg ");
Imgupdown. src = images [arguments. callee. em ^ = 1];
// Hide the lower p
Var content = $ (". hidecontent ");
// Determine the operation based on the display attribute
If (content.css ("display ")! = "None "){
Content. slideUp ("slow ");
} Else {
Content. slideDown ("slow ");;
}
}
Functionchangeimg (){
// Change the image
Var images = ['images/up.png ', 'images/down.png']
Var imgupdown = document. getElementById ("hideimg ");
Imgupdown. src = images [arguments. callee. em ^ = 1];
// Hide the lower p
Var content = $ (". hidecontent ");
// Determine the operation based on the display attribute
If (content.css ("display ")! = "None "){
Content. slideUp ("slow ");
} Else {
Content. slideDown ("slow ");;
}
}


Here, the image address is placed in an array, which is an exclusive or operation. The array parameter is automatically selected to switch the image.
The hidden display of content uses the. CSS attribute.
The following is: (to be supplemented)

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.