JQuery Click Show again click Hide

Source: Internet
Author: User

<HTML>    <Head>        <Scripttype= "Text/javascript"src= "/jquery/jquery.js"></Script>    </Head>    <Body>        <Div>            <spanclass= "Color">Dark Curry</span>            <Divclass= "CC"></Div>        </Div>        <Div>            <spanclass= "Size">11*11</span>            <Divclass= "BB"></Div>        </Div>    </Body></HTML>            
 1  .color,.size  { Font-size : 14px ;  color : black ;} 2  .cc  {: 300px ; height : 100px ; background : red ;} 3  .bb  {: 300px ; height : 100px ; background : red ;} 

The first is simple to show and hide, using bind () and toggle ();

1 $ (function() {2      $ (". Color"). Bind ("click",function() {$ (  this). Next (". CC"). Toggle ();}); 3      $ (". Size"). Bind ("click",function() {$ (this). Next (". BB"). Toggle ();}); 4 });

The toggle () method has its own display of hidden effects.

But the disadvantage of this JS is that when I click on the "Dark color" to display the red box, click "11*11" again, the green block appears, but the red box will not be hidden,

The second way: to improve the above shortcomings, when you click again, hide the other, only click the corresponding content appears.

$ (function() { 
$ (". Color"). Bind ("click",function() { $ (this). Next (". CC"). Show (); $ (". BB"). Hide (); }); $ (". Size"). Bind ("click",function() { $ (this). Next (". BB"). Show () ; $ (". CC"). Hide (); }) ;

Third, on the basis of the second, when you click on the display, the corresponding content appears, click on the other disappears, or click on the other blank space to disappear.

$(function(){        $(". Color"). Bind ("click",function(){       $( This). Next (". CC")). Show (); $(". BB"). Hide ();       }); $(". Size"). Bind ("click",function(){       $( This). Next (". BB")). Show (); $(". CC"). Hide (); }); $(". Color"). On ("click",function(e) {$ (document). One ("Click",function(){        $(". CC"). Hide (); $(". BB"). Hide ();    }); E.stoppropagation ();}); $(". CC"). On ("click",function(e) {e.stoppropagation ();}); $(". Size"). On ("click",function(e) {$ (document). One ("Click",function(){        $(". BB"). Hide (); $(". CC"). Hide ();    }); E.stoppropagation ();}); $(". BB"). On ("click",function(e) {e.stoppropagation ();}); });

The one () method attaches a single or more event handlers to the selected element and specifies the function to run when the event occurs.

When using the one () method, the event handler function can only be run once per element.

JQuery Click Show again click Hide

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.