可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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...
This is preview Div2 to show after div 1 hides.
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