2-2 下拉菜单--属性声明式方法(一)
本节编程练习不计算学习进度,请电脑登录imooc.com操作

下拉菜单--属性声明式方法(一)

属性声明式方法:

一般下拉菜单都是出现在导航条中,比如下图的一个效果,用户点击带有三角形的菜单项都会弹出下拉菜单项:

<div class="navbar navbar-default" id="navmenu">
    <a href="##" class="navbar-brand">W3cplus</a>
    <ul class="nav navbar-nav">
        <li class="dropdown">
            <a href="##" data-toggle="dropdown" class="dropdown-toggle" role="button" id="tutorial">教程<b class="caret"></b></a>
            <ul class="dropdown-menu" role="menu" aria-labelledby="tutorial">
                <li role="presentation"><a href="##">CSS3</a></li>
                <li role="presentation"><a href="##">HTML5</a></li>
                <li role="presentation"><a href="##">Sass</a></li>
            </ul>
        </li>
        <li><a href="##">前端论坛</a></li>
        <li><a href="##">关于我们</a></li>
    </ul>
</div>

除了这种导航条之外,在胶囊式导航中也具有下拉菜单,其结构如:

<ul class="nav nav-pills">
    <li class="dropdown">
        <a href="##" data-toggle="dropdown" class="dropdown-toggle" role="button" id="tutorial">教程<b class="caret"></b></a>
        <ul class="dropdown-menu" role="menu" aria-labelledby="tutorial">
            <li role="presentation"><a href="##">CSS3</a></li>
            <li role="presentation"><a href="##">HTML5</a></li>
            <li role="presentation"><a href="##">Sass</a></li>
        </ul>
    </li>
    <li class="active"><a href="##">前端论坛</a></li>
    <li><a href="##">关于我们</a></li>
</ul>

运行效果如下:

任务

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>下拉菜单--属性声明式方法(一)</title>
  6. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  7. <link rel="stylesheet" href="style.css">
  8. </head>
  9. <body>
  10. <h3>示例1</h3>
  11. <div class="navbar navbar-default" id="navmenu">
  12. <a href="##" class="navbar-brand">W3cplus</a>
  13. <ul class="nav navbar-nav">
  14. <li class="dropdown">
  15. <a href="##" data-toggle="dropdown" class="dropdown-toggle" role="button" id="tutorial">教程<b class="caret"></b></a>
  16. <ul class="dropdown-menu" role="menu" aria-labelledby="tutorial">
  17. <li role="presentation"><a href="##">CSS3</a></li>
  18. <li role="presentation"><a href="##">HTML5</a></li>
  19. <li role="presentation"><a href="##">Sass</a></li>
  20. </ul>
  21. </li>
  22. <li><a href="##">前端论坛</a></li>
  23. <li><a href="##">关于我们</a></li>
  24. </ul>
  25. </div>
  26.  
  27.  
  28. <h3>示例2</h3>
  29. <ul class="nav nav-pills">
  30. <li class="dropdown">
  31. <a href="##" data-toggle="dropdown" class="dropdown-toggle" role="button" id="tutorial">教程<b class="caret"></b></a>
  32. <ul class="dropdown-menu" role="menu" aria-labelledby="tutorial">
  33. <li role="presentation"><a href="##">CSS3</a></li>
  34. <li role="presentation"><a href="##">HTML5</a></li>
  35. <li role="presentation"><a href="##">Sass</a></li>
  36. </ul>
  37. </li>
  38. <li class="active"><a href="##">前端论坛</a></li>
  39. <li><a href="##">关于我们</a></li>
  40. </ul>
  41. <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
  42. <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  43. </body>
  44. </html>
下一节