JQuery小練習,JQuery練習
1、加法計算機。兩個文字框中輸入數字,點擊【=】按鈕將相加的結果放到第三個文字框中。<script src="jquery-1.8.3.js"></script> <script type="text/javascript"> $(function () { $('#btn').click(function () { var first = parseInt($('#btnFirst').val()); var second = parseInt($('#btnSecond').val()); var sum = first + second; $('#btnResult').val(sum); }); }); </script></head><body> <input type="text" name="name" value="" id="btnFirst" />+ <input type="text" name="name" value="" id="btnSecond" /> <input type="button" name="name" value="=" id="btn" /> <input type="text" name="name" value="" id="btnResult" /></body>2、十秒鐘後協議文字框下的註冊按鈕才能點擊,時鐘倒數。設定可用性等jQuery未封裝方法:attr("")setInterval() <script type="text/javascript"> $(function () { var i = 5; var setId = setInterval(function () { i--; if (i <= 0) { $('#btnShow').val('同意').attr('disabled', false); clearInterval(setId); } else { $('#btnShow').val('請仔細閱讀協議(' + i + ')'); } }, 500); }); </script>
<input type="button" name="name" value="請仔細閱讀協議(5)" id="btnShow" disabled="disabled" />3、無重新整理評論。(已做) <script src="jquery-1.8.3.js"></script> <script type="text/javascript"> $(function () { $("#btn1").click(function () { //建立一行和兩個單元格 $('<tr><td>' + $('#txt').val() + '</td><td>' + $('#textContent').val() + '</td></tr>').appendTo($("#tb")); }); }); </script></head><body> <table id="tb" style="border:1px solid red;"> <tr> <td> 貓貓: </td> <td> 沙發耶! </td> </tr> </table> <br /> 暱稱:<input type="text" id="txt" value="" /><br /> <textarea id="textContent" rows="10" cols="15"></textarea><br /> <input type="button" value="評論" id="btn1" /></body>4、案例1:建立若干個輸入文字框,當游標離開文字框的時候如果文字框為空白,則將文字框背景色設定為紅色,如果不為空白則為白色。提示:焦點進入控制項的事件是focus,焦點離開控制項的事件是blur。 $(function () { $('input').blur(function () { if ($(this).val().length == 0) { $(this).css('backgroundColor', 'red'); } else { $(this).css('backgroundColor',''); } }); });5、案例:選擇球隊,兩個ul。被懸浮行高亮顯示(背景是紅色),點擊球隊將它放到另一個的球隊列表。//清除所有事件。.unbind();<script type="text/javascript"> $(function () { //滑鼠經過 $('#uul li').mouseover(function () { $(this).css('cursor', 'pointer').css('backgroundColor', 'red'); //滑鼠離開 }).mouseout(function () { $(this).css('cursor', 'pointer').css('backgroundColor', '') //滑鼠點擊 .click(function () { $(this).unbind().removeAttr('style').appendTo($('#ul2')); }); }) }); </script>1、加法計算機。兩個文字框中輸入數字,點擊【=】按鈕將相加的結果放到第三個文字框中。<script src="jquery-1.8.3.js"></script> <script type="text/javascript"> $(function () { $('#btn').click(function () { var first = parseInt($('#btnFirst').val()); var second = parseInt($('#btnSecond').val()); var sum = first + second; $('#btnResult').val(sum); }); }); </script></head><body> <input type="text" name="name" value="" id="btnFirst" />+ <input type="text" name="name" value="" id="btnSecond" /> <input type="button" name="name" value="=" id="btn" /> <input type="text" name="name" value="" id="btnResult" /></body>2、十秒鐘後協議文字框下的註冊按鈕才能點擊,時鐘倒數。設定可用性等jQuery未封裝方法:attr("")setInterval() <script type="text/javascript"> $(function () { var i = 5; var setId = setInterval(function () { i--; if (i <= 0) { $('#btnShow').val('同意').attr('disabled', false); clearInterval(setId); } else { $('#btnShow').val('請仔細閱讀協議(' + i + ')'); } }, 500); }); </script>
<input type="button" name="name" value="請仔細閱讀協議(5)" id="btnShow" disabled="disabled" />3、無重新整理評論。(已做) <script src="jquery-1.8.3.js"></script> <script type="text/javascript"> $(function () { $("#btn1").click(function () { //建立一行和兩個單元格 $('<tr><td>' + $('#txt').val() + '</td><td>' + $('#textContent').val() + '</td></tr>').appendTo($("#tb")); }); }); </script></head><body> <table id="tb" style="border:1px solid red;"> <tr> <td> 貓貓: </td> <td> 沙發耶! </td> </tr> </table> <br /> 暱稱:<input type="text" id="txt" value="" /><br /> <textarea id="textContent" rows="10" cols="15"></textarea><br /> <input type="button" value="評論" id="btn1" /></body>4、案例1:建立若干個輸入文字框,當游標離開文字框的時候如果文字框為空白,則將文字框背景色設定為紅色,如果不為空白則為白色。提示:焦點進入控制項的事件是focus,焦點離開控制項的事件是blur。 $(function () { $('input').blur(function () { if ($(this).val().length == 0) { $(this).css('backgroundColor', 'red'); } else { $(this).css('backgroundColor',''); } }); });5、案例:選擇球隊,兩個ul。被懸浮行高亮顯示(背景是紅色),點擊球隊將它放到另一個的球隊列表。//清除所有事件。.unbind();<script type="text/javascript"> $(function () { //滑鼠經過 $('#uul li').mouseover(function () { $(this).css('cursor', 'pointer').css('backgroundColor', 'red'); //滑鼠離開 }).mouseout(function () { $(this).css('cursor', 'pointer').css('backgroundColor', '') //滑鼠點擊 .click(function () { $(this).unbind().removeAttr('style').appendTo($('#ul2')); }); }) }); </script>