初入javascript知識點(七)

來源:互聯網
上載者:User

標籤:ice   turn   register   exti   cte   清除   top   list   .ajax   

用jQuery重寫之前的student資料

html

<!DOCTYPE html>

 

姓名: 年齡: 性別: 添加

 

姓名 年齡 性別 操作

 

JS

$(function() {  var data;  // 1. 擷取資料  if(localStorage.data === undefined) {    $.ajax({      url: ‘./../data/data.json‘,      type: ‘get‘,      success: function(dataFromServer) {        data = dataFromServer;        localStorage.data = JSON.stringify(data);        showTable();      }    });  } else {    data = JSON.parse(localStorage.data);    showTable();  }  // 新增學生  $(‘#add‘).on(‘click‘, function() {    var stu = {      name: $(‘#name‘).val(),      age: $(‘#age‘).val(),      gender: $(‘#gender‘).val()    };    $(‘input‘).val(‘‘);    data.push(stu);    localStorage.data = JSON.stringify(data);    showTable();  });  // 刪除  $(‘#tbody‘).on(‘click‘, ‘.delete‘, function() {    var index = this.parentNode.parentNode.sectionRowIndex;    // 刪掉data數組中對應的那一項    data.splice(index, 1);    localStorage.data = JSON.stringify(data);    showTable();  });  // 修改姓名  $(‘#tbody‘).on(‘click‘, ‘.name‘, function() {    onChangeBtnClick(this, 0, ‘name‘);  });  // 修改年齡  $(‘#tbody‘).on(‘click‘, ‘.age‘, function() {    onChangeBtnClick(this, 1, ‘age‘);  });  // 點擊修改XX按鈕  function onChangeBtnClick(btn, tdIndex, prop) {    var $tr = $(btn).parent().parent();    var $td = $tr.children().eq(tdIndex);    // console.log($td);    $td.html(‘<input type="text" value="‘ + $td.text() + ‘">‘);    var $operateTd = $(btn).parent();    $operateTd      .empty()      .append($(‘<button>確定</button>‘).on(‘click‘, function() {        var newValue = $td.children().eq(0).val();        var index = $tr.get(0).sectionRowIndex;        // console.log(newValue, index);        data[index][prop] = newValue;        localStorage.data = JSON.stringify(data);        showTable();      }))      .append($(‘<button class="cancel">取消</button>‘))  }  // 取消修改  $(‘#tbody‘).on(‘click‘, ‘.cancel‘, showTable);  // 顯示表格  function showTable() {    $(‘#tbody‘).empty();    $.each(data, function(i, n) {      // console.log(i);      // console.log(n);      // console.log(this);      // console.log(‘========‘);      $(‘<tr></tr>‘)        .append($(          ‘<td>‘ + n.name + ‘</td>‘          + ‘<td>‘ + n.age + ‘</td>‘          + ‘<td>‘ + n.gender + ‘</td>‘          + ‘<td><button class="delete">刪除</button><button class="name">修改姓名</button><button class="age">修改年齡</button></td>‘))        .appendTo($(‘#tbody‘))    });  }});

jQuery寫輪播

$(function() {  $(‘#wrap‘).data(‘index‘, 0);  $(‘#nav‘).on(‘click‘, ‘li‘, function() {    // 清除自動播放的定時器    clearInterval(timer);    var that = this;    var index = $(this).index();    // 把當前要顯示的圖片的索引號,儲存到附加資料中    $(‘#wrap‘).data(‘index‘, index).animate({      left: index * (-430)    }, 600, function() {      $(‘#nav li‘).removeClass(‘selected‘);      $(that).addClass(‘selected‘);      // 重啟自動播放的定時器      timer = setInterval(autoPlay, 2000);    });  });  var timer = setInterval(autoPlay, 2000);  function autoPlay() {    // console.log(123);    // 擷取當前的index    var currentIndex = $(‘#wrap‘).data(‘index‘);    // 下一張的index    var nextIndex;    if(currentIndex === $(‘#wrap img‘).length - 1) {      nextIndex = 0;    } else {      nextIndex = currentIndex + 1;    }    // console.log(nextIndex);    $(‘#wrap‘).data(‘index‘, nextIndex).animate({      left: nextIndex * (-430)    }, 600, function() {      $(‘#nav li‘).removeClass(‘selected‘).eq(nextIndex).addClass(‘selected‘);    });  }});
handlebars 模版引擎
<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>handlebars</title>  <style>    .big {      width: 100px;      height: 100px;      border: 1px solid green;      border-radius: 100px;      position: relative;    }    .small {      width: 50px;      height: 50px;      border: 1px solid green;      border-radius: 50px;      position: absolute;      left: 0;      right: 0;      top: 0;      bottom: 0;      margin: auto;      text-align: center;      line-height: 50px;    }  </style></head><body>  <div id="container"></div>  <script id="template" type="text/x-handlebars-template">    <h1>Hello</h1>    <h2>Handlebars</h2>    <table border="1">      <thead>        <tr>          <th>姓名</th>          <th>年齡</th>          <th>性別</th>        </tr>      </thead>      <tbody>        <tr>          <td>{{name}}</td>          <td>{{age}}</td>          <td>{{gender}}</td>        </tr>      </tbody>    </table>    <p>hello, {{name}}</p>    <p>{{hello}}</p>    <table border="1">      <thead>        <tr>          <th>姓名</th>          <th>年齡</th>          <th>性別</th>        </tr>      </thead>      <tbody>        {{#each listOfStudents}}          <tr>            <td>{{name}}</td>            <td>{{age}}</td>            <td>{{gender}}</td>          </tr>        {{/each}}      </tbody>    </table>    <p>hello, {{{person.a.name}}}</p>    <p>hello, {{person/a/name}}</p>    <div class="big">      <div class="small">        2      </div>    </div>    <div class="big">      <div class="small">        3      </div>    </div>    <div class="big">      <div class="small">        4      </div>    </div>    {{circle name}}    {{circle name}}    {{circle name}}  </script>  <script src="./js/handlebars-v4.0.5.js"></script>  <script>    // 擷取模板的原始碼    var source = document.getElementById(‘template‘).innerHTML;    // 把模板的原始碼,編譯成模板對象    var template = Handlebars.compile(source);    // 建立helper    Handlebars.registerHelper(‘circle‘, function(data) {      // return ‘<div class="big"><div class="small">‘ + data + ‘</div></div>‘;      // 告訴系統,這個傳回值要解析成真正的標籤      var obj = new Handlebars.SafeString(‘<div class="big"><div class="small">‘ + data + ‘</div></div>‘);      return obj;    });    // 通過模板對象,擷取最終html代碼    var html = template({      person: {        a: {          name: ‘Tom<input type="text">‘        },        b: ‘hello‘      },      name: ‘Bob‘,      age: 20,      gender: ‘male‘,      test: ‘hello‘,      listOfStudents: [        {          name: ‘bob‘,          age: 20,          gender: ‘male‘        },        {          name: ‘tom‘,          age: 22,          gender: ‘male‘        },        {          name: ‘Hellen‘,          age: 20,          gender: ‘female‘        },      ]    });    // console.log(html);    // 把html代碼插入到網頁中去    document.getElementById(‘container‘).innerHTML = html;    // helper  </script></body></html>

初入javascript知識點(七)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.