如何在show / hide切换中仅使用“show”?

时间:2013-04-08 12:59:29

标签: javascript jquery css html5 css3

使用以下脚本在按钮上显示/隐藏div

function showHide(divID){
if (document.getElementById(divID).style.display == "none") {
    $('#'+divID).fadeIn(3000);

} else {
    $('#'+divID).fadeOut(3000);
}
}

这是我的HTML:

<button onClick="showHide('hideDiv',this.id)" type="button">English</button>
<button onClick="showHide('hideDiv',this.id)" type="button">Math</button>
<button onClick="showHide('hideDiv',this.id)" type="button">French</button>

并使用单个div显示单击按钮的内容

<div id="hideDiv" style="display:none;">
    <p>A painting workshop in the early Renaissance probably resembled</p>
</div>

当我在英语后单击数学时,内容将隐藏,再次单击后,内容将再次显示。 但是,我想在用户点击任何按钮时显示内容,我想隐藏“隐藏”属性,这样用户就可以看到他点击的按钮内容。

这里是小提琴链接http://jsfiddle.net/ytyAd/

3 个答案:

答案 0 :(得分:1)

以下是我认为您正在寻找的内容...... http://jsfiddle.net/ytyAd/10/

JS把它放在$(document).ready():

$("button.showHide").click( function() {
    var content = $(this).text();
    $("#hideDiv > p").hide("slow");
    $("#hideDiv #"+content).show("slow");
});

HTML:

<button class="showHide" type="button">English</button>
<button class="showHide" type="button">Math</button>
<button class="showHide" type="button">French</button>

<div id="hideDiv">
    <p id="English" style="display:none;">english stuff</p>
    <p id="Math" style="display:none;">math stuff</p>
    <p id="French" style="display:none;">french stuff</p>
</div>

当然你可以玩它(不同的动画,回调等),使它适合你的背景

答案 1 :(得分:0)

你需要这样的东西吗?

Live Demo Here

<强> HTML

<button id="engbutton" class="button" type="button">English</button>
<button id="mathbutton" class="button" type="button">Math</button>
<button id="frenchbutton" class="button" type="button">French</button>
<div id="engbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is English Div</p>
</div>
<div id="mathbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is Maths Div</p>
</div>
<div id="frenchbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is French Div</p>
</div>

<强> CSS

.ContentDiv
{
    height:50px;
    width:200px;
    background:green;
}

<强>的jQuery

  $(".button").click(function(){
        $(".ContentDiv").hide();
        var divid = $(this).attr("id") + "Div";
        if($("#"+divid).css('display') == 'none')
        {
            $("#"+divid).fadeIn(3000);
        }
        else
        {
            $("#"+divid).fadeOut(3000);
        }  
    });

答案 2 :(得分:0)

HTML:

<div class="buttons">
<button id="engbutton" class="button" type="button">English</button>
<button id="mathbutton" class="button" type="button">Math</button>
<button id="frenchbutton" class="button" type="button">French</button>
</div>
<div class="contents">    
<div id="engbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is English Div</p>
</div>
<div id="mathbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is Maths Div</p>
</div>
<div id="frenchbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is French Div</p>
</div>
</div>

JS:

 $('.button').on('click', function(){
        $('.contents').find('div').hide();
        var getMessageDiv = '#'+$(this).attr('id')+"Div";
        if ($(getMessageDiv).is(':visible'))
        $(getMessageDiv).fadeOut();
         else 
        $(getMessageDiv).fadeIn();
    })

<强> LIVE Demo

相关问题