Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript第一步.pdf

Avatar for Thomas chang Thomas chang
March 26, 2018
51

 JavaScript第一步.pdf

Avatar for Thomas chang

Thomas chang

March 26, 2018

Transcript

  1. 基本的宣告方式 變數的宣告方式 var a = 1; var b = 'abc';

    陣列的宣告方式 var list = ['a', 'b', 'c', 'def'];
  2. Html 範列 <html> <head> <title>Hello World!</title> <meta name="description" content="I am

    learning to code."/> </head> <body> <h2>Hi There!</h2> <p id="first">You are all right?</p> <p id="second">abcd</p> <input id="btn" type="button" value="click" /> </body> </html>
  3. 操縱DOM var first = document.getElementById('first'); var second = document.getElementById('second'); var

    btn = document.getElementById('btn'); btn.addEventListener('click', clickHandler); function clickHandler() { first.innerHTML = 'JavaScript';screentogif second.style.color = '#f00'; } https://jsfiddle.net/ujxmk01p/
  4. 實際使用jQuery來操縱DOM var $first = $('#first'); var $second = $('#second'); $('#btn').on('click',

    clickHandler); function clickHandler() { $first.html('JavaScript'); $second.css('color', '#f00'); } https://jsfiddle.net/zuuhg118/
  5. var $first = $('#first'); var $second = $('#second'); $('#btn').on('click', clickHandler);

    function clickHandler() { $first.html('JavaScript'); $second.css('color', '#f00'); } 實際使用jQuery來操縱DOM 使用CSS Selector #來選取
  6. var $first = $('#first'); var $second = $('#second'); $('#btn').on('click', clickHandler);

    function clickHandler() { $first.html('JavaScript'); $second.css('color', '#f00'); } 實際使用jQuery來操縱DOM 習慣在變數前面加上$, 代表這是jQuery物件
  7. var $first = $('#first'); var $second = $('#second'); $('#btn').on('click', clickHandler);

    function clickHandler() { $first.html('JavaScript'); $second.css('color', '#f00'); } 實際使用jQuery來操縱DOM 使用On來註冊click事件
  8. var $first = $('#first'); var $second = $('#second'); $('#btn').on('click', clickHandler);

    function clickHandler() { $first.html('JavaScript'); $second.css('color', '#f00'); } 實際使用jQuery來操縱DOM jQuery有很多方法都是可以設定資 料或是取得資料 例如: $first.html('JavaScript'); //設定元素P 的內容 $first.html(); //取得元素P的內容
  9. var $first = $('#first'); var $second = $('#second'); $('#btn').on('click', clickHandler);

    function clickHandler() { $first.html('JavaScript'); $second.css('color', '#f00'); } 實際使用jQuery來操縱DOM css也是同樣的,設定 或取得樣式
  10. jQuery其他常用方法 $('#myTbl td:even').css({ backgroundColor: '#000', color: '#fff' }); $('#myTbl td').each(function(i)

    { $(this).html((i % 3) + 1); }); E F 是選取E底下的F元素, F可以是E的子元素的子元 素,#myTbl td也就是選取 #myTbl底下的所有的td。 :even 由選取結果中只挑出 偶數列
  11. jQuery其他常用方法 $('#myTbl td:even').css({ backgroundColor: '#000', color: '#fff' }); $('#myTbl td').each(function(i)

    { $(this).html((i % 3) + 1); }); css也可以用傳入物件的方 式,如果要設定多個值,可 以這樣使用
  12. jQuery其他常用方法 $('#myTbl td:even').css({ backgroundColor: '#000', color: '#fff' }); $('#myTbl td').each(function(i)

    { $(this).html((i % 3) + 1); }); each如同for迴圈一樣,將 選取的元素執行想要的動 作,這邊是填入1到3的數 字。 this指的就是td
  13. jQuery其他常用方法 $('#matrix td').on('click', function() { var $td = $(this); $td.parents('table:first').find('td')

    .css('background-color', 'white'); var $tr = $td.parent(); $tr.children().css('background-color', 'red'); var nthChild = $tr.children().index($td) + 1; $tr.siblings().find('td:nth-child(' + nthChild + ')') .css('background-color', 'green'); $td.css('background-color', 'black'); });
  14. jQuery其他常用方法 $('#matrix td').on('click', function() { var $td = $(this); $td.parents('table:first').find('td')

    .css('background-color', 'white'); var $tr = $td.parent(); $tr.children().css('background-color', 'red'); var nthChild = $tr.children().index($td) + 1; $tr.siblings().find('td:nth-child(' + nthChild + ')') .css('background-color', 'green'); $td.css('background-color', 'black'); }); parents是選取父元素,這 邊是取得table find是搜索符合條件的子元 素,這邊是取得table下的所 有td 這段程式是將所有格子全 部塗白
  15. jQuery其他常用方法 $('#matrix td').on('click', function() { var $td = $(this); $td.parents('table:first').find('td')

    .css('background-color', 'white'); var $tr = $td.parent(); $tr.children().css('background-color', 'red'); var nthChild = $tr.children().index($td) + 1; $tr.siblings().find('td:nth-child(' + nthChild + ')') .css('background-color', 'green'); $td.css('background-color', 'black'); }); 用parent()取得<td> 所屬<tr>,全列塗紅
  16. jQuery其他常用方法 $('#matrix td').on('click', function() { var $td = $(this); $td.parents('table:first').find('td')

    .css('background-color', 'white'); var $tr = $td.parent(); $tr.children().css('background-color', 'red'); var nthChild = $tr.children().index($td) + 1; $tr.siblings().find('td:nth-child(' + nthChild + ')') .css('background-color', 'green'); $td.css('background-color', 'black'); }); 以index()找出是第幾欄 ,稍後要用於nth-child, 以1起算,故先加1,並且 使用siblings取得表格所 有其他<tr>, 將它們塗 綠
  17. jQuery其他常用方法 $('#matrix td').on('click', function() { var $td = $(this); $td.parents('table:first').find('td')

    .css('background-color', 'white'); var $tr = $td.parent(); $tr.children().css('background-color', 'red'); var nthChild = $tr.children().index($td) + 1; $tr.siblings().find('td:nth-child(' + nthChild + ')') .css('background-color', 'green'); $td.css('background-color', 'black'); }); 最後將自已填成黑色