如何在按钮中显示/隐藏div

时间:2013-10-05 01:24:05

标签: jquery navigation show-hide mobile-devices

我使用jquery幻灯片切换来在单击bttn时显示/隐藏div。是否可以将div放入按钮? (我需要这个能够将导航正确地粘贴在用于移动设备的页面顶部。)

HTML

<div class="medianavbttn">Show Menu</div>

<div class="medianav">
    <ul id="medianav-links">
        <li class="current"><a href="#section1">About</a></li>
        <li><a href="#section2">Gallery Photos</a></li>
        <li><a href="#section3">Contact</a></li>
    </ul>
</div>

剧本

<script>
$(".medianavbttn").on("click", function(){
    $(".medianav").slideToggle("slow");
    $(this).text($(this).text() == "Show Menu" ? "Hide Menu" : "Show Menu");
});
</script>

CSS

.medianavbttn {
position: relative;
display:block;
width: 100%;
height: 30px;
top: 0;
left: 0;
cursor: pointer;
}

.medianav {
position: relative;
display: none;
width: 100%;
}

1 个答案:

答案 0 :(得分:0)

这是你要找的吗?
JsFiddle http://jsfiddle.net/GGtkc/
HTML

<div class="medianavbttn">
    <span class="showbutton">Show Menu</span>

<div class="medianav">
    <ul id="medianav-links">
        <li class="current"><a href="#section1">About</a></li>
        <li><a href="#section2">Gallery Photos</a></li>
        <li><a href="#section3">Contact</a></li>
    </ul>
</div>
</div>

<强> JS:

$(".medianavbttn").on("click", function(){
    $(".medianav").slideToggle("slow");
    $(this).find('.showbutton').text($(this).find('.showbutton').text() == "Show Menu" ? "Hide Menu" : "Show Menu");
});