zoukankan      html  css  js  c++  java
  • [jQuery] $.map, $.each, detach() , $.getJSOIN()

    $.map function will return the modifies array.

    $.each function will not new a new array, the old value will still be the same.

    detach() funciton will remove the element from the DOM, you can append those element later, it works more eiffient.

    getJSON

    $('button').on('click', function() {
      $.getJSON('/cities/deals', function(result){
          $.each(result, function(index, dealItem) {
            var dealElement = $('.deal-' + index);
            dealElement.find('.name').html(dealItem.name);
            dealElement.find('.price').html(dealItem.price);
          });
      });
    });

    Let's take a minute to make our previous code a bit more efficient. Use the .detach() method to remove the .flight-times list element from the DOM before you insert the new listitems. Then, re-insert the .flight-times element back into the .flights element.

    $('.update-available-flights').on('click', function() {
      $.getJSON('/flights/late', function(result) {
        var flightElements = $.map(result, function(flightItem, index){
          var flightEl = $('<li>'+flightItem.flightNumber+'-'+flightItem.time+'</li>');
          return flightEl;
        });
        $('.flight-times').detach().html(flightElements).appendTo('.flights');
      });
    });
  • 相关阅读:
    c++函数学习-关于c++函数的林林总总
    STL学习笔记(七) 程序中使用STL
    STL学习笔记(六) 函数对象
    本学期总结与课程建议
    12.19
    12.18Tomcat相关知识
    12.17
    12.16
    12.15
    12.14
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3928426.html
Copyright © 2011-2022 走看看