【问题标题】:Target a Specific Child of an Element Using jQuery使用 jQuery 定位元素的特定子元素
【发布时间】:2018-03-17 01:44:55
【问题描述】:

当我尝试使用以下方法定位第二个孩子时:

 $(".well:nth-child(2)").addClass("animated bounce"); 

它针对的是所有孩子,而不是我希望它针对的特定孩子。

命令有什么问题?

 <script> $(document).ready(function() {
$("#target1").css("color", "red");
$("#target1").prop("disabled", true);
$("#target4").remove();
$("#target2").appendTo("#right-well");
$("#target5").clone().appendTo("#left-well");
$("#target1").parent().css("background-color", "red");
$("#right-well").children().css("color", "orange");

    $(".well:nth-child(2)").addClass("animated bounce");
});
</script>

<!-- Only change code above this line. -->

<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
  <div class="row">
<div class="col-xs-6">
  <h4>#left-well</h4>
  <div class="well" id="left-well">
    <button class="btn btn-default target" id="target1">#target1</button>
    <button class="btn btn-default target" id="target2">#target2</button>
    <button class="btn btn-default target" id="target3">#target3</button>
  </div>
</div>
<div class="col-xs-6">
  <h4>#right-well</h4>
  <div class="well" id="right-well">
    <button class="btn btn-default target" id="target4">#target4</button>
    <button class="btn btn-default target" id="target5">#target5</button>
    <button class="btn btn-default target" id="target6">#target6</button>
  </div>
</div>

更改代码:

$(".well:nth-child(2)").addClass("animated bounce");     

$(".target:nth-child(2)").addClass("animated bounce"); 

我得到了只针对第二个孩子完成所需的工作

这个命令有什么错误

$(".well:nth-child(2)").addClass("animated bounce"); 

为什么要针对所有的孩子?

【问题讨论】:

  • 使用$(".target:first-child + .target").addClass("animated bounce");

标签: jquery css html jquery-mobile


【解决方案1】:
  1. 这将选择包含.well 类的任何元素的第二个子元素

    .well :nth-child(2)

  2. 否则,这将选择任何具有 .well 类且是其父级的第二个子级的元素

    .well:nth-child(2)

应该使用这个:

$(".well :nth-child(2)").addClass("animated bounce"); 

注意选择器中.well:nth-child(2) 之间的空格' '

$(".well :nth-child(2)").addClass("animated bounce");

$("#target1").css("color", "red");
$("#target1").prop("disabled", true);
$("#target4").remove();
$("#target2").appendTo("#right-well");
$("#target5").clone().appendTo("#left-well");
$("#target1").parent().css("background-color", "red");
$("#right-well").children().css("color", "orange");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
<h3 class="text-primary text-center">jQuery Playground</h3>
  <div class="row">
<div class="col-xs-6">
  <h4>#left-well</h4>
  <div class="well" id="left-well">
    <button class="btn btn-default target" id="target1">#target1</button>
    <button class="btn btn-default target" id="target2">#target2</button>
    <button class="btn btn-default target" id="target3">#target3</button>
  </div>
</div>
<div class="col-xs-6">
  <h4>#right-well</h4>
  <div class="well" id="right-well">
    <button class="btn btn-default target" id="target4">#target4</button>
    <button class="btn btn-default target" id="target5">#target5</button>
    <button class="btn btn-default target" id="target6">#target6</button>
  </div>
</div>

【讨论】:

    【解决方案2】:

    您可以使用$(".target:first-child + .target").addClass("animated bounce");

    $("#target1").css("color", "red");
    $("#target1").prop("disabled", true);
    $("#target4").remove();
    $("#target2").appendTo("#right-well");
    $("#target5").clone().appendTo("#left-well");
    $("#target1").parent().css("background-color", "red");
    $("#right-well").children().css("color", "orange");
    
        $(".target:first-child + .target").addClass("animated bounce");
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!-- Only change code above this line. -->
    
    <div class="container-fluid">
    <h3 class="text-primary text-center">jQuery Playground</h3>
      <div class="row">
    <div class="col-xs-6">
      <h4>#left-well</h4>
      <div class="well" id="left-well">
        <button class="btn btn-default target" id="target1">#target1</button>
        <button class="btn btn-default target" id="target2">#target2</button>
        <button class="btn btn-default target" id="target3">#target3</button>
      </div>
    </div>
    <div class="col-xs-6">
      <h4>#right-well</h4>
      <div class="well" id="right-well">
        <button class="btn btn-default target" id="target4">#target4</button>
        <button class="btn btn-default target" id="target5">#target5</button>
        <button class="btn btn-default target" id="target6">#target6</button>
      </div>
    </div>

    【讨论】:

    • 他要求解释,你的回答解释了发生了什么吗?
    【解决方案3】:
     $(".well:nth-child(2)").addClass("animated bounce"); 
    

    你需要一个指定的子元素来定位元素“target”。

    这是正确的方法:

     $(".well button.btn.target:nth-child(2)").addClass("animated bounce"); 
    

    【讨论】:

    • 他要求解释,你的回答解释了发生了什么吗?
    猜你喜欢
    • 2019-12-21
    • 2019-05-15
    • 2019-08-09
    • 1970-01-01
    • 2023-03-12
    • 2012-11-11
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    相关资源
    最近更新 更多