Files
freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/jquery/target-the-same-element-with-multiple-jquery-selectors.md
Oliver Eyton-Williams ee1e8abd87 feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

4.0 KiB
Raw Blame History

id, title, challengeType, forumTopicId, required, dashedName
id title challengeType forumTopicId required dashedName
bad87fee1348bd9aed908626 用多个 jQuery 选择器选择同一个元素 6 18322
link
https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.css
target-the-same-element-with-multiple-jquery-selectors

--description--

现在你知道三种选取标签的方法:用标签选择器,如$("button");用类选择器,$(".btn")以及用 id 选择器,$("#target1")

虽然可以在单个.addClass()内添加多个类,但是我们可以用三种不同的方式给一种标签添加类。

以三种不同的方式用.addClass()方法每次只给一种标签添加一个类:

给所有的button标签添加animated类。

给所有类为.btnbutton标签添加shake类。

给所有 id 为#target1button标签添加btn-primary类。

注意:
虽然三个选择器最终给 id 为#target1button标签添加shakeanimatedbtn-primary等三个类,但是你需要用且仅用三种不同的选择器给三种标签各添加一个类(译者注:所谓的“一种标签”是指他们有某种共同的特点,如包含同一个 class

--hints--

$('button')选择标签。

assert(code.match(/\$\s*?\(\s*?(?:'|")\s*?button\s*?(?:'|")/gi));

$('.btn')选择标签。

assert(code.match(/\$\s*?\(\s*?(?:'|")\s*?\.btn\s*?(?:'|")/gi));

$('#target1')选择标签。

assert(code.match(/\$\s*?\(\s*?(?:'|")\s*?#target1\s*?(?:'|")/gi));

三个选择器每个只添加一个类。

assert(
  code.match(/addClass/g) &&
    code.match(/addClass\s*?\(\s*?('|")\s*?[\w-]+\s*?\1\s*?\)/g).length > 2
);

#target1标签应具有animatedshakebtn-primary三个类。

assert(
  $('#target1').hasClass('animated') &&
    $('#target1').hasClass('shake') &&
    $('#target1').hasClass('btn-primary')
);

仅用 jQuery 给标签添加类。

assert(!code.match(/class.*animated/g));

--seed--

--seed-contents--

<script>
  $(document).ready(function() {

  });
</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>
  </div>
</div>

--solutions--

<script>
  $(document).ready(function() {
    $("button").addClass("animated");
    $(".btn").addClass("shake");
    $("#target1").addClass("btn-primary");
  });
</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>
  </div>
</div>