前端笔记我爱编程

使用jQuery获取元素

2018-04-12  本文已影响6人  好奇男孩

jQuery选择器

2.png 3.png 4.png 5.png

jQuery还提供了一些方法帮助我们查找当前元素相关元素

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<div id="header">
 <div class="nav">
    <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
   <p class="ok">ok</p>
 </div>
</div>
  <div class="main">
     <div class="content">
    <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
   <p class="ok">ok</p>
  </div>
</body>
</html>
$(".child").eq(2)
init [div.child, prevObject: init(8), context: document]
$(".child").eq(0).next()
init [div.child, prevObject: init(1), context: document]0: div.childcontext: documentlength: 1prevObject: init [div.child, prevObject: init(8), context: document]__proto__: Object(0)
$(".child").eq(1).prev()
init [div.child, prevObject: init(1), context: document]0: div.childcontext: documentlength: 1prevObject: init [div.child, prevObject: init(8), context: document]__proto__: Object(0)
$(".child").eq(1).prev('ok')
init [prevObject: init(1), context: document]
$(".child").eq(1).prevAll('')
init [div.child, prevObject: init(1), context: document]0: div.childcontext: documentlength: 1prevObject: init [div.child, prevObject: init(8), context: document]__proto__: Object(0)
$(".child").eq(1).nextAll('')
init(3) [div.child, div.child, p.ok, prevObject: init(1), context: document]
$(".child").eq(1).siblings('')
init(4) [div.child, div.child, div.child, p.ok, prevObject: init(1), context: document]
$(".child").eq(1).parent('.nav')
init [div.nav, prevObject: init(1), context: document]
$(".child").eq(1).parents('#header')
init [div#header, prevObject: init(1), context: document]
$('.nav').children('.child')
init(4) [div.child, div.child, div.child, div.child, prevObject: init(1), context: document]
$('#header').find('.child')
init(4) [div.child, div.child, div.child, div.child, prevObject: init(1), context: document, selector: "#header .child"]
$('.child').filter(":even")
init(4) [div.child, div.child, div.child, div.child, prevObject: init(8), context: document]
$('.child').filter(function(index) {
  return index % 3 === 0;
})
init(3) [div.child, div.child, div.child, prevObject: init(8), context: document]
$('.nav').has("div")
init [div.nav, prevObject: init(1), context: document]
$('.nav').is(".nav")
true

范例:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.js"></script>
    <title></title>
    <style>
        *{
            box-sizing: border-box;
        }
        .main{
            margin-top: 50px;
        }
        .main .header:after{
            content: '';
            display: block;
            clear: both;
        }
        .main .header .tab{
            float: left;
            border-left: 1px solid #ccc;
            border-top: 1px solid #ccc;
            padding: 10px 20px;
            background-color: #dddddd;
            cursor: pointer;
        }
        .main .header .tab:last-child{
            border-right: 1px solid #ccc;
        }
        .main .header .tab.active{
            background-color: #fff;
        }
        .main .content{
            border: 1px solid #ccc;
            background-color: #fff;
            height: 200px;
            width: 400px;
        }
        .main .theme{
            display: none;
            background-color: #fff;
            padding: 100px;
        }
        .main .theme.active{
            display: block;
        }

    </style>
</head>
<body>
<div class="main">
    <div class="header">
        <div class="tab active">1</div>
        <div class="tab ">2</div>
        <div class="tab">3</div>
        <div class="tab">4</div>
    </div>
    <div class="content">
        <div class="theme active">one</div>
        <div class="theme ">two</div>
        <div class="theme">three</div>
        <div class="theme">four</div>
    </div>
</div>
<script>
    $(".main  .tab").on('click',function(){
        console.log(this);
        $(this).addClass('active')
                .siblings()
                .removeClass('active')

        $(this).parents('.main')
                .find('.theme')
                .eq($(this).index())
                .addClass('active')
                .siblings()
                .removeClass('active')
    })

</script>
</body>
</html>
      function $$(select){
          return document.querySelectorAll(select)
      }
      $$(".main .tab").forEach(function(node){
          node.addEventListener('click',function(){
              var index;
              this.parentElement.querySelectorAll(".tab").forEach(function(e,x){
                  e.classList.remove('active');
                  if(node===e){
                     index=x;
                  }
              });
              this.classList.add('active');
              this.parentElement.nextElementSibling.querySelectorAll(".theme").forEach(function(e){
                  e.classList.remove("active")
              });
              this.parentElement.nextElementSibling.querySelectorAll('.theme')[index].classList.add('active')
          })
})

使用使用 jquery能更方便,且简化我们的代码

注意:jQuery 1.x 版本和 2.x 版本有什么区别?

上一篇下一篇

猜你喜欢

热点阅读