NBR-hugh / freecodecamp-practice

Practice everyday
0 stars 0 forks source link

JSON APIs and Ajax (2 hours) #20

Open NBR-hugh opened 8 years ago

NBR-hugh commented 8 years ago

JSON

NBR-hugh commented 8 years ago
NBR-hugh commented 8 years ago

项目

Trigger Click Events with jQuery total:10min

Change Text with Click Events TOTAL:10MIN

Get JSON with the jQuery getJSON Method TOTAL:35MIN

Convert JSON Data to HTML TOTAL:50MIN

Render Images from Data Sources TOTAL:13MIN

Prefilter JSON TOTAL:5MIN

Get Geo-location Data TOTAL:15MIN

NBR-hugh commented 8 years ago

Trigger Click Events with jQuery 使用jQuery 单击触发事件

要点

完整程序

<script>
  $(document).ready(function() {
    // Only change code below this line.
    $("#getMessage").on("click",function(){});

    // Only change code above this line.
  });
</script>

<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
    </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>

记录

NBR-hugh commented 8 years ago

Change Text with Click Events 用点击事件改编文本

要点

<div class = "col-xs-12 well message">
      The message will go here
    </div>

完整程序:

<script>
  $(document).ready(function() {
    $("#getMessage").on("click", function(){
      // Only change code below this line.
      $(".message").html("Here is the message");
      // Only change code above this line.
    });
  });
</script>

<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
    </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>

记录

NBR-hugh commented 8 years ago

Get JSON with the jQuery getJSON Method 获得JOSN

要点

问题

按要求手动添加

  $.getJSON("/json/cats.json",function(json){
        $(".message").html(JOSN.stringify(json));
      });

结果无法通过测试,于是直接复制黏贴命令看是否是输入错误,通过挑战,确定为输入错误。 原命令为:

$.getJSON("/json/cats.json", function(json) {
  $(".message").html(JSON.stringify(json));
});

发现错误为将JSON 输成 JOSN,戒之戒之!!!

完整程序

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

    $("#getMessage").on("click", function(){
      // Only change code below this line.
    $.getJSON("/json/cats.json",function(json){
        $(".message").html(JSON.stringify(json));
      });
      // Only change code above this line.
    });

  });
</script>

<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message" >
      The message will go here
    </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>

记录

NBR-hugh commented 8 years ago

Convert JSON Data to HTML 将JSON数据传递给HTML

要点

程序

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

    $("#getMessage").on("click", function() {
      $.getJSON("/json/cats.json", function(json) {

        var html = "";
        // Only change code below this line.
        json.forEach(function(val){
          var keys =Object.keys(val);
          html +="<div class='cat'>";
          keys.forEach(function(key){
               html += "<strong>"+ key +"</strong>"+ val[key]+"<br>";
                       });
          html +="</div><br>";
        });
        // Only change code above this line.

        $(".message").html(html);

      });
    });
  });
</script>

<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
   </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>

实现效果

记录

NBR-hugh commented 8 years ago

Render Images from Data Sources 从数据源提取图片

要点

问题

程序

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

    $("#getMessage").on("click", function() {
      $.getJSON("/json/cats.json", function(json) {

        var html = "";

        json.forEach(function(val) {

          html += "<div class = 'cat'>";

          // Only change code below this line.

          html += "<img src = '"+val.imageLink + "'" +"alt='" + val.altText + "'>";

          // Only change code above this line.

          html += "</div>";

        });

        $(".message").html(html);

      });
    });
  });
</script>

<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
    </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>

记录

NBR-hugh commented 8 years ago

Prefilter JSON 预先过滤 JSON

要点

json = json.filter(function(val){
          return(val.id !== 1 );
        })

程序

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

    $("#getMessage").on("click", function() {
      $.getJSON("/json/cats.json", function(json) {

        var html = "";

        // Only change code below this line.
        json = json.filter(function(val){
          return(val.id !== 1 );
        })

        // Only change code above this line.

        json.forEach(function(val) {

          html += "<div class = 'cat'>"

          html += "<img src = '" + val.imageLink + "' " + "alt='" + val.altText + "'>"

          html += "</div>"

        });

        $(".message").html(html);

      });
    });
  });
</script>

<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
    </div>
  </div>
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message
      </button>
    </div>
  </div>
</div>

记录

NBR-hugh commented 8 years ago

Get Geolocation Data 获取地理数据

要点

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
    $("#data").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);
  });
}

程序

<script>
  // Only change code below this line.
  if (navigator.geolication){
    navigator.geolocation.getCurrentPosition(function(position){
     $("#data").html("latitude+" + position.coords.latitude +"<br>longitude:"+position.coords.longitude);
    });
  }

  // Only change code above this line.
</script>
<div id = "data">
  <h4>You are here:</h4>

</div>

记录

NBR-hugh commented 8 years ago