<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