Hide Div Until Link Is Selected

雨燕双飞 提交于 2019-12-12 05:00:01

问题


I have a hide/show navigation working but can't seem to figure out how to hide the main div that contains all the children divs until one of the links in the navigation is selected.

Please see my fiddle: http://jsfiddle.net/blahblahAMYblah/KptZ6/

To be clear, I do not just want all the children divs (ie. about, gallery, etc.) hidden at the start, but the main div hidden. Once a link is selected, then the main div should show with only the selected child div showing as well.


回答1:


I've updated your fiddle;

http://jsfiddle.net/KptZ6/2/

If you don't want to change the HTML, the following will hide the main div using javascript. It also hides the children the same way (using .hide())

http://jsfiddle.net/KptZ6/5/

$(function() {
    $('#main').hide();
});

$('body').on('click','nav a', function(e) {
    $('#main').show();
    $('#main').children().hide();
    $($(this).attr('href')).show();
    e.preventDefault();
});​



回答2:


Just hide the div initially then show it when an item is clicked

$('body').on('click','nav a', function(e) {
    $('#main').css('display', 'block');
    $('#main').children().addClass('hide');
    $($(this).attr('href')).removeClass('hide');
    e.preventDefault();
});

#main{
    display:none;
}

DEMO




回答3:


not sure if you want something like this, here is an update:

http://jsfiddle.net/KptZ6/3/



来源:https://stackoverflow.com/questions/12168914/hide-div-until-link-is-selected

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