1.代碼實例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>05-jQuery- 回到頂部案例</title> <style> .bk-top-box { position: fixed; bottom: 40px; right: 10px; cursor: pointer; } </style> <script src="jquery-1.11.3.js"></script> <script> $(function(){ $("#J-bk-top").on('click',function(){ $(window).scrollTop(0); $(window).scrollLeft(0); }); $("#J_Box").on('click','h1',function(){ //this jQuery的事件響應方法中,this指向被點擊的 dom對象 this.innerHTML += '-'; //dom對象轉換成 jQuery對象 $(this).text($(this).text() + 1); }); }); </script> </head> <body> <div class="bk-top-box" id="J-bk-top"> <img src="imgs/bk-top3.jpg" height="44" width="44" alt=""> </div> <!-- ctrl + shift + g 自動包裹標簽 --> <div id="J_Box"> <h1>傳智播客1</h1> <h1>傳智播客2</h1> <h1>傳智播客3</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> <h1>傳智播客</h1> </div> </body> </html>
2.實際效果