我如何在选择器中使用它

时间:2016-03-15 07:11:37

标签: javascript jquery

<style>
    .disNone{display:none;}
    .disBlock{display:block;}
</style>

<script>
    $(".visitlink.item span").click(function(){
        $(".summary.item").toggleClass("disNone");
        $(".fullsum.item").toggleClass("disBlock");  
    });
</script>

<div class="wrapper">
    <div class="coursebox">
        <div class="summary item">summary</div>
        <div class="fullsum item">fullsum</div>
        <div class="visitlink item"><span>Readmore</span></div>
    </div>
    <div class="coursebox">
        <div class="summary item">summary</div>
        <div class="fullsum item">fullsum</div>
        <div class="visitlink item"><span>Readmore</span></div>
    </div>
</div>

如何点击div点击div它正在同时处理 $m = new MongoClient('mongodb://localhost', [ 'username' => 'abc', 'password' => 'abc@123', 'db' => 'abc' ]); 如何编写代码来解决此问题。

2 个答案:

答案 0 :(得分:1)

工作演示

$(".visitlink.item span").click(function() {
  $(this).closest(".coursebox").find(".summary.item").toggleClass("disNone");
  $(this).closest(".coursebox").find(".fullsum.item").toggleClass("disBlock");
  
});
.disNone {
  display: none;
}
.disBlock {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">

  <div class="coursebox">
    <div class="summary item">summary</div>
    <div class="fullsum item">fullsum</div>

    <div class="visitlink item"><span>Readmore</span>
    </div>

  </div>

  <div class="coursebox">
    <div class="summary item">summary</div>
    <div class="fullsum item">fullsum</div>

    <div class="visitlink item"><span>Readmore</span>
    </div>

  </div>

</div>

答案 1 :(得分:0)

您可以更改为

 $(this).parent().siblings(".summary.item").toggleClass("disNone");
 $(this).parent().siblings(".fullSum.item").toggleClass("disBlock");

因为被点击的元素是span元素,而目标元素是它的父div的兄弟。