06 01 action JavaScript action jquery jquery AJAX 04 4-1 CSS jquery CSS jquery HTML CSS jquery.css() getter setter.css('backgroundcolor') jquery CSS background-color camel-cased DOM backgroundcolor.css() map.css('property','value').css({property1: 'value1', 'property-2': 'value2'}) JavaScript jquery JavaScript object literal 72
06 jquery Note DOM.css().addClass() HTML <div id="switcher"> <div class="label">style Switcher</div> <div class="button" id="switcher-default">default</div> <div class="button" id="switcher-large">bigger</div> <div class="button" id="switcher-small">smaller</div> </div> <div class="speech"> <p>fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.</p> </div> 01 04-1 CSS 4-1 div Bigger Smaller <div class= "speech"> Default <div class= "speech"> 73
06 01 04.addClass() 40% $(document).ready() $('#switcher-large').click() $('#switcher-large').click(function() {.css() $('div.speech').css('fontsize') px jquery jquery var $speech = $('div.speech'); $('#switcher-large').click(function() { var num = parsefloat($speech.css('fontsize'), 10);.click() speech div $(document).ready() <div class="speech"> $speech $ $ JavaScript jquery.click() parsefloat() parsefloat() 12 12 74
06 jquery parsefloat() NaN parsefloat num 1.4 num px var $speech = $('div.speech'); $('#switcher-large').click(function() { var num = parsefloat($speech.css('fontsize'), 10); num *= 1.4; $speech.css('fontsize', num + 'px'); 01 04-1 Note num *= 1.4 num = num * 1.4 num += 1.4 num -= 1.4 num /= 1.4 num %= 1.4 Bigger CSS 4-2 Bigger 75
06 01 04 Smaller num /= 1.4 <div id="switcher"> <button>.click() ID var $speech = $('div.speech'); $('#switcher button').click(function() { var num = parsefloat($speech.css('fontsize'), 10); if (this.id == 'switcher-large') { num *= 1.4; } else if (this.id == 'switcher-small') { num /= 1.4; } $speech.css('fontsize', num + 'px'); 3 this DOM id if else if this jquery DOM Default else if switch var $speech = $('div.speech'); var defaultsize = $speech.css('fontsize'); $('#switcher button').click(function() { var num = parsefloat($speech.css('fontsize'), 10); switch (this.id) { case 'switcher-large': num *= 1.4; break; 76
06 jquery case 'switcher-small': num /= 1.4; break; default: num = parsefloat(defaultsize, 10); } $speech.css('fontsize', num + 'px'); this.id 'switcher-large' 'switcher-small' 01 04-2 4-2.hide().show().css('display', 'string') string.hide() style display:none display none display block inline.show() display display: none Note display Mozilla Developer Center https://developer.mozilla.org/en/css/display https://developer.mozilla.org/samples/cssref/display.html 77
06 01 04.show().hide() display <li> display:block display:inline <li>.show() display:block <li>.show() display:inline HTML <div id="switcher"> <div class="label">text Size</div> <button id="switcher-default">default</button> <button id="switcher-large">bigger</button> <button id="switcher-small">smaller</button> </div> <div class="speech"> <p>fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal. </p> <p>now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field as a final resting-place for those who here gave their lives that the nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow, this ground. </p> <a href="#" class="more">read more</a> </div> DOM 78
06 jquery $('p:eq(1)').hide(); 4-3 read more $('p:eq(1)').hide(); $('a.more').click(function() { $('p:eq(1)').show(); $(this).hide(); return false; 01 04-2 { $(document).ready(function() 4-4 read more.hide().show() 79