使用jQuery定位元素的特定子元素

时间:2017-10-05 12:27:38

标签: jquery html css html5 jquery-mobile

当我尝试使用以下目标攻击第二个孩子时:

 $(".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"); 

为什么针对所有孩子?

3 个答案:

答案 0 :(得分:0)

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

&#13;
&#13;
$("#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");
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

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

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

这是正确的方式:

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

答案 2 :(得分:0)

  1. 这将选择包含.well

    的任何元素的第二个子元素

    .well :nth-child(2)

  2. 否则,这将选择具有.well类&amp;的任何元素。这是其父母的第二个孩子

    .well:nth-child(2)

  3. 应该使用这个:

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

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

    &#13;
    &#13;
    $(".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");
    &#13;
    <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>
    &#13;
    &#13;
    &#13;

相关问题