1、tab切换显示块

  1. 设置class名基础样式
  2. 利用for循环赋予所有li点击事件
  3. 点击后先将所有兄弟元素的class名清空,再赋予点击的元素class名使其获得样式
  4. 获取正在被点击的元素的下标
  5. 先将所有.content下的div块隐藏,再利用被点击元素的下标设置对应的div块显示
    1. <style>
    2. .current{
    3. color: orange;
    4. }
    5. .content{
    6. width: 100px;
    7. height: 100px;
    8. border: 1px solid #333;
    9. position: relative;
    10. }
    11. .content>div{
    12. width: 100%;
    13. height: 100%;
    14. position: absolute;
    15. }
    16. .content>div:first-child{
    17. background: red;
    18. z-index: 100;
    19. }
    20. .content>div:last-child{
    21. background: yellow;
    22. }
    23. </style>
    24. </head>
    25. <body>
    26. <ul>
    27. <li class="current">登录</li>
    28. <li>注册</li>
    29. <div class="content">
    30. <div>登录的内容</div>
    31. <div>注册的内容</div>
    32. </div>
    33. </ul>
    34. <script>
    35. /* 1、点击对应的li给对应的li添加calss = "current" ,并给它的兄弟元素移除class="current"*/
    36. var lis = document.querySelectorAll("ul li");
    37. var divs = document.querySelectorAll(".content div");
    38. for(var i = 0;i<lis.length;i++){ //赋予所有li点击事件
    39. lis[i].index = i; //获取正在被点击的元素下标
    40. lis[i].onclick = function(){
    41. for(var i=0;i<lis.length;i++){
    42. lis[i].className = ""; //先将所有元素class名清空
    43. }
    44. this.className = "current"; //赋予点击的元素class名
    45. /* 2、让所有的.content下的div隐藏,点击对应的下标的div,对应的元素显示 */
    46. console.log(this.index);
    47. for(var i=0;i<divs.length;i++){
    48. divs[i].style.display = "none"; //先将所有元素隐藏
    49. }
    50. divs[this.index].style.display = "block"; //赋予点击的元素显示
    51. }
    52. }
    53. </script>
    54. </body>
    image.pngimage.png

    2、jquery

    2-1.安装jquery与使用

    下载jquery包放在文件目录下利用script引入
    html-css bootstrap
    JavaScript jquery
    jquery 是一个JavaScript的库
    1、查询DOM
    2、Ajax
    1. <!--
    2. html-css bootstrap
    3. JavaScript jquery
    4. jquery 是一个JavaScript的库
    5. 1、查询DOM
    6. 2、Ajax
    7. -->
    8. <div id="app">div</div>
    9. <script>
    10. /* $ --> 找到 */
    11. var app = $("#app");
    12. console.log(app)
    13. </script>
    image.png

    2-2.jquery支持使用所有CSS选择器

    允许获取时使用所有CSS选择器
    1. <ul class="parent">
    2. <li>1</li>
    3. <li>2</li>
    4. <li>3</li>
    5. <li>4</li>
    6. </ul>
    7. <script>
    8. /* jquery支持所有CSS选择器 */
    9. var lis = $(".parent li");
    10. console.log(lis)
    image.png

    2-3.jquery点击事件

    1. <div id="app">div</div>
    2. <script>
    3. $("#app").click(function(){
    4. $(this).html("change")
    5. })
    6. </script>
    image.pngimage.png

    2-4.jquery-focus事件与blur事件

    1. <input type="text" id="app">
    2. <script>
    3. $("#app").focus(function(){
    4. $(this).css({backgroundColor:"red",width:"100px"})
    5. })
    6. $("#app").blur(function(){
    7. $(this).css({backgroundColor:"yellow"})
    8. })
    9. </script>
    image.pngimage.png

    2-5.链式调用

    jquery允许在单个获取后接着调用
    1. <input type="text" id="app">
    2. <script>
    3. $("#app").focus(function(){
    4. $(this).css({backgroundColor:"red",width:"100px"})
    5. }).blur(function(){
    6. $(this).css({backgroundColor:"yellow"})
    7. })
    8. </script>

    2-6.消失效果

    .hide() 隐藏
    .show() 显示
    1. <div>hello world</div>
    2. <button id="btn">按钮</button>
    3. <script>
    4. $("#btn").click(function(){
    5. $("div").hide() //括号内可写数字设置隐藏时间
    6. })
    7. </script>
    image.pngimage.png

    2-7.切换效果

    .is(“:visible”)
    1. <div>hello world</div>
    2. <button id="btn">按钮</button>
    3. <script>
    4. $("#btn").click(function(){
    5. /* 判断元素是否显示 */
    6. /* .is(":visible") 判断元素是否隐藏 */
    7. var isShow = $("div").is(":visible");
    8. console.log(isShow)
    9. if(isShow){
    10. $("div").hide(2000)
    11. }else{
    12. $("div").show(2000)
    13. }
    14. })
    15. </script>
    image.pngimage.pngimage.png

    2-8.toggle切换

    .toggle 切换
    1. <div>hello world</div>
    2. <button id="btn">按钮</button>
    3. <script>
    4. $("#btn").click(function(){
    5. /* toggle:切换 融合hide和show方法 */
    6. $("div").toggle()
    7. })
    8. </script>
    image.pngimage.pngimage.png

    2-9.ready加载完毕

    $(document).ready(function(){} —等待页面加载完毕再执行下面代码
    ==window.onload
    tips:如果后面的js依赖于jquery,必须写在jquery后面
    1. // window.onload = function(){
    2. // $("div").click(function(){
    3. // console.log(1);
    4. // })
    5. // }
    6. $(document).ready(function(){
    7. /* 等待页面加载完毕再执行下面代码 */
    8. $("div").click(function(){
    9. console.log(1)
    10. })
    11. })

    2-10.siblings()获取兄弟元素

    .siblings()获取所有兄弟元素
    1. <ul class="parent">
    2. <li>1</li>
    3. <li class="two">2</li>
    4. <li>3</li>
    5. <li>4</li>
    6. </ul>
    7. <script>
    8. var siblings = $(".two").siblings();
    9. console.log(siblings);
    10. console.log($(".parent").children());
    11. </script>
    image.png

    2-11.获取指定位置的元素

    .eq(index)
    改变指定位置的元素,从0开始,如果是负数则从最后开始倒数
    1. <script>$('li').eq(2).css('background-color', 'red');</script>
    image.png