如何使用jquery在Toggle(隐藏/显示)按钮中首先隐藏内容

时间:2015-09-21 07:35:55

标签: jquery html

我用jquery做了一个切换按钮。我打开那个页面,首先我看到所有内容都可见。当我点击按钮然后它隐藏。我想先隐藏,然后点击按钮然后显示。

http://imgur.com/a/YxrOg

第一张图片是点击之前 第二个是点击后

HTML

 <button id="pak">Asia</button>
<div class="asia">
 <ul>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/aisa.html">&nbsp;&nbsp;&nbsp;&nbsp;<font size="4">List of cities in Asia</font></a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/shag.html">&nbsp;&nbsp;&nbsp;&nbsp;Shanghai</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/toky.html">&nbsp;&nbsp;&nbsp;&nbsp;Tokyo</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/kara.html">&nbsp;&nbsp;&nbsp;&nbsp;Karachi</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/dehl.html">&nbsp;&nbsp;&nbsp;&nbsp;Dehli</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/mumb.html">&nbsp;&nbsp;&nbsp;&nbsp;Mumbai</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/beij.html">&nbsp;&nbsp;&nbsp;&nbsp;Beijing</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/seou.html">&nbsp;&nbsp;&nbsp;&nbsp;Seoul</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/hong.html">&nbsp;&nbsp;&nbsp;&nbsp;Hong Kong</a></li> 
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/gaun.html">&nbsp;&nbsp;&nbsp;&nbsp;Gaungzhou</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/uzbe.html">&nbsp;&nbsp;&nbsp;&nbsp;Uzbekestian</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/tehr.html">&nbsp;&nbsp;&nbsp;&nbsp;Tehran</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/laho.html">&nbsp;&nbsp;&nbsp;&nbsp;Lahore</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/riya.html">&nbsp;&nbsp;&nbsp;&nbsp;Riyadh</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/jedd.html">&nbsp;&nbsp;&nbsp;&nbsp;Jeddah</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/jakt.html">&nbsp;&nbsp;&nbsp;&nbsp;Jaktara</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/osak.html">&nbsp;&nbsp;&nbsp;&nbsp;Osaka</a></li>
  <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/tian.html">&nbsp;&nbsp;&nbsp;&nbsp;Tianjin</a></li>
</ul>
</div>

的Javascript

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#pak").click(function(){
        $(".asia").toggle(700);
    });
});
</script>

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

将样式display:none添加到元素或类中,它将隐藏页面加载元素

$(document).ready(function() {
  $("#pak").click(function() {
    $(".asia").toggle(700);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="pak">Asia</button>
<div class="asia" style="display:none">
  <ul>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/aisa.html">&nbsp;&nbsp;&nbsp;&nbsp;<font size="4">List of cities in Asia</font></a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/shag.html">&nbsp;&nbsp;&nbsp;&nbsp;Shanghai</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/toky.html">&nbsp;&nbsp;&nbsp;&nbsp;Tokyo</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/kara.html">&nbsp;&nbsp;&nbsp;&nbsp;Karachi</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/dehl.html">&nbsp;&nbsp;&nbsp;&nbsp;Dehli</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/mumb.html">&nbsp;&nbsp;&nbsp;&nbsp;Mumbai</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/beij.html">&nbsp;&nbsp;&nbsp;&nbsp;Beijing</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/seou.html">&nbsp;&nbsp;&nbsp;&nbsp;Seoul</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/hong.html">&nbsp;&nbsp;&nbsp;&nbsp;Hong Kong</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/gaun.html">&nbsp;&nbsp;&nbsp;&nbsp;Gaungzhou</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/uzbe.html">&nbsp;&nbsp;&nbsp;&nbsp;Uzbekestian</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/tehr.html">&nbsp;&nbsp;&nbsp;&nbsp;Tehran</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/laho.html">&nbsp;&nbsp;&nbsp;&nbsp;Lahore</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/riya.html">&nbsp;&nbsp;&nbsp;&nbsp;Riyadh</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/jedd.html">&nbsp;&nbsp;&nbsp;&nbsp;Jeddah</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/jakt.html">&nbsp;&nbsp;&nbsp;&nbsp;Jaktara</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/osak.html">&nbsp;&nbsp;&nbsp;&nbsp;Osaka</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/tian.html">&nbsp;&nbsp;&nbsp;&nbsp;Tianjin</a>
    </li>
  </ul>
</div>

答案 1 :(得分:0)

您可以使用简单的CSS规则隐藏它们。

CSS推荐

.asia{
    display:none;
}

或者,使用jQuery隐藏它们

$(document).ready(function(){
    $(".asia").hide();
    $("#pak").click(function(){
        $(".asia").toggle(700);
    });
});

$(document).ready(function() {
  $("#pak").click(function() {
    $(".asia").toggle(700);
  });
});
.asia {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="pak">Asia</button>
<div class="asia">
  <ul>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/aisa.html">&nbsp;&nbsp;&nbsp;&nbsp;<font size="4">List of cities in Asia</font></a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/shag.html">&nbsp;&nbsp;&nbsp;&nbsp;Shanghai</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/toky.html">&nbsp;&nbsp;&nbsp;&nbsp;Tokyo</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/kara.html">&nbsp;&nbsp;&nbsp;&nbsp;Karachi</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/dehl.html">&nbsp;&nbsp;&nbsp;&nbsp;Dehli</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/mumb.html">&nbsp;&nbsp;&nbsp;&nbsp;Mumbai</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/beij.html">&nbsp;&nbsp;&nbsp;&nbsp;Beijing</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/seou.html">&nbsp;&nbsp;&nbsp;&nbsp;Seoul</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/hong.html">&nbsp;&nbsp;&nbsp;&nbsp;Hong Kong</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/gaun.html">&nbsp;&nbsp;&nbsp;&nbsp;Gaungzhou</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/uzbe.html">&nbsp;&nbsp;&nbsp;&nbsp;Uzbekestian</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/tehr.html">&nbsp;&nbsp;&nbsp;&nbsp;Tehran</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/laho.html">&nbsp;&nbsp;&nbsp;&nbsp;Lahore</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/riya.html">&nbsp;&nbsp;&nbsp;&nbsp;Riyadh</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/jedd.html">&nbsp;&nbsp;&nbsp;&nbsp;Jeddah</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/jakt.html">&nbsp;&nbsp;&nbsp;&nbsp;Jaktara</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/osak.html">&nbsp;&nbsp;&nbsp;&nbsp;Osaka</a>
    </li>
    <li><a href="file:///C:/Documents%20and%20Settings/maheer/Desktop/cities/tian.html">&nbsp;&nbsp;&nbsp;&nbsp;Tianjin</a>
    </li>
  </ul>
</div>