Hide particular div onload and then show div after click

匿名 (未验证) 提交于 2019-12-03 00:45:01

问题:

I have two divs div1 and div2. I want div2 to be automatically hidden but when i click on preview div then div2 to be made visible and div1 to hide. This is the code i tried but no luck :(

This is preview Div1. This is preview Div1.
This is preview Div2 to show after div 1 hides.
PREVIEW

回答1:

Make sure to watch your selectors. You appear to have forgotten the # for div2. Additionally, you can toggle the visibility of many elements at once with .toggle():

// Short-form of `document.ready` $(function(){     $("#div2").hide();     $("#preview").on("click", function(){         $("#div1, #div2").toggle();     }); }); 

Demo: http://jsfiddle.net/dJg8N/



回答2:

This is an easier way to do it. Hope this helps...

PREVIEW
  • If you want the div to be hidden on load, make the style display:none
  • Use toggle rather than click function.


Links:

JQuery Tutorials

JQuery References



回答3:

You are missing # hash character before id selectors, this should work:

$(document).ready(function() {     $("#div2").hide();      $("#preview").click(function() {       $("#div1").hide();       $("#div2").show();     });  }); 

Learn More about jQuery ID Selectors



回答4:

The second time you're referring to div2, you're not using the # id selector.

There's no element named div2.



回答5:

$(document).ready(function() {     $('#div2').hide(0);     $('#preview').on('click', function() {         $('#div1').hide(300, function() { // first hide div1             // then show div2             $('#div2').show(300);         });          }); }); 

You missed # before div2

Working Sample



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!