jQuery

01 - Learn how Script Tags and Document Ready Work

  • 先添加 document ready function

  • 若沒有添加 $(document).ready(function() { });, js 會在 html 渲染前就先執行,這樣會產生 bug

<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>

02 - Target HTML Elements with Selectors Using jQuery

  • $ dollar sign operator, bling

  • 選擇器

  • 添加跳動動畫

  • 已經預載 jQuery.js, animated bounce.css

<script>
  $(document).ready(function() {
    $("button").addClass("animated bounce");
  });
</script>

03-Target Elements by Class Using jQuery

  • 替 div.well 添加 shake 特效

<script>
  $(document).ready(function() {
    $("button").addClass("animated bounce");
    $(".well").addClass("animated shake")
  });
</script>

04-Target Elements by ID Using jQuery

  • 替 div#target3 添加 fadeout 特效

<script>
  $(document).ready(function() {
    $("button").addClass("animated bounce");
    $(".well").addClass("animated shake");
    $("#target3").addClass("animated fadeout")
  });
</script>

05-Delete your jQuery Functions

  • 教學說 jQurey 的特效會讓人分心,要我先把三個語句都刪掉 XD

06-Target the same element with multiple jQuery Selectors

  • 從前面知道了 type: $("button"), class:$(".btn"), id:$("target3") 三種選擇方式

  • 讓一個元素能被 jQuery Selectors 選擇多次

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

07-Remove classes from an element with jQuery

  • 移除某個元素 or 選擇器的 class

  • 但我根本看不出效果啊 @@

  • 有啦就是 button 從灰色變成白色

<script>
  $(document).ready(function() {
    $("button").addClass("animated bounce");
    $(".well").addClass("animated shake");
    $("#target3").addClass("animated fadeOut");
    $("button").removeClass("btn-default");  
  });
</script>

08-Change the CSS of an Element Using jQuery

  • 用 jQuery 改變 css

  • 有 quote,用 comma instead of colon

<script>
  $(document).ready(function() {
    $("#target1").css("color", "red");

  });
</script>

09-Disable an Element Using jQuery

  • 直接用 jQuery 修改除了 css 以外的元素的屬性

  • 例如,讓按鈕不可選

<script>
  $(document).ready(function() {
    $("#target1").css("color", "red");
    $("#target1").prop("disabled", true);
  });
</script>

10-Change Text Inside an Element Using jQuery

  • 覆蓋某個元素中的 html 內容

  • .html()

<script>
  $(document).ready(function() {
    $("#target1").css("color", "red");
    $("#target4").html("<em>#target4</em>");
  });
</script>

11-Remove an Element Using jQuery

  • 移除某個元素

  • .remove()

<script>
  $(document).ready(function() {
    $("#target1").css("color", "red");
    $("#target1").prop("disabled", true);
    $("#target4").remove();
  });
</script>

12-Use appendTo to Move Elements with jQuery

  • 把 div 中的某個元素,移動到另外一個 div 中

  • appendTo()

<script>
  $(document).ready(function() {
    $("#target1").css("color", "red");
    $("#target1").prop("disabled", true);
    $("#target4").remove();
    $("#target2").appendTo("#right-well");
  });
</script>

13-Clone an Element Using jQuery

  • 複製元素

  • clone()

  • function chaining,方法鏈,串起兩個方法

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

14-Target the Parent of an Element Using jQuery

  • 每個元素都有父元素

  • 可用 parent() 訪問父元素

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

15-Target the Children of an Element Using jQuery

  • 訪問該元素底下的所有子元素

  • children()

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

16-Target a Specific Child of an Element Using jQuery

<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");
    $(".target:nth-child(2)").addClass("animated bounce");
  });
</script>

17-Target Even Numbered Elements Using jQuery

  • 選擇索引為奇數或是偶數的元素

<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");
    $("#left-well").children().css("color", "green");
    //$(".target:nth-child(2)").addClass("animated bounce");
    $(".target:even").addClass("animated shake"); 
  });
</script>

18-Use jQuery to Modify the Entire Page

  • 整個頁面 $("body")

  • fadeout, hinge

<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");
    $("#left-well").children().css("color", "green");
    $(".target:nth-child(2)").addClass("animated bounce");
    $(".target:even").addClass("animated shake");
    $("body").addClass("animated hinge");
  });
</script>

Last updated

Was this helpful?