简体   繁体   中英

Multiple ID's in jquery/javascript

I have this segment of code

$('#test').click(function(){
 $(this).hide();
 $('#hidethis').show();
});
$('#test2').click(function(){
  $('#hidethis').hide();
  $('#test').show();
})

This Code works fine, I'm however wanting to apply it to more elements. So i want to ideally add more ID's

I have tried this method which hasn't worked.

$('#test,#test3').each.click(function(){
 $(this).hide();
 $('#hidethis').show();
});
$('#test2,#test4').each.click(function(){
  $('#hidethis').hide();
  $('#test').show();
})

Will add a Fiddle if needed

All you need to do is the following:

$('#test, #test3').click(function(){
 $(this).hide();
 $('#hidethis').show();
});

$('#test2, #test4').click(function(){
  $('#hidethis').hide();
  $('#test').show();
});

JSFIDDLE EXAMPLE

 $('.test').click(function () { $('.test').each(function(){ $(this).hide(); }); $('#hidethis').show(); }); $('#hidethis').click(function () { $(this).hide(); $('.test').show(); }) 
 .test { display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="hidethis">HIDE THIS</div> <div class="test">SHOW ME</div> <div class="test">SHOW ME1</div> <div class="test">SHOW ME2</div> <div class="test">SHOW ME3</div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM