国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

一個JavaScript操作元素定位元素的實例

2024-05-06 16:10:10
字體:
供稿:網(wǎng)友

操作元素定位元素,用js來實現(xiàn)是個不錯的選擇,下面有個示例,需要的朋友可以看看

 

 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  5. <title>每天一個JavaScript實例-操作元素定位元素</title>  
  6. <style>  
  7. div#a{  
  8. width:500px;  
  9. }  
  10. div{  
  11. border:1px solid #000;  
  12. padding:10px;  
  13. }  
  14. #cursor{  
  15. position:absolute;  
  16. background-color:#ff0;  
  17. width:20px;  
  18. height:20px;  
  19. left:50px;  
  20. top:300px;  
  21. }  
  22. </style>  
  23. <script>  
  24. function positionObject(obj){  
  25. var rect = obj.getBoundingClientRect();  
  26. return [rect.left,rect.top];  
  27. }  
  28. window.onload = function(){  
  29. var tst = document.documentElement.getBoundingClientRect();  
  30. alert(tst.top);  
  31. var cont = 'A';  
  32. var cursor = document.getElementById("cursor");  
  33. while(cont){  
  34. cont = prompt("where do you want to move the cursor block?","A");  
  35. if(cont){  
  36. cont = cont.toLowerCase();  
  37. if(cont == "a"||cont=="b"||cont=="c"){  
  38. var elem = document.getElementById(cont);  
  39. var pos = positionObject(elem);  
  40. console.log(pos);  
  41. cursor.setAttribute("style","top:"+pos[1]+"px;"+"left:"+pos[0]+"px");  
  42. }  
  43. }  
  44.  
  45. }  
  46. }  
  47. </script>  
  48. </head>  
  49.  
  50. <body>  
  51.  
  52. <div id = "a">  
  53. <p>A</p>  
  54. <div id ="b">  
  55. <p>B</p>  
  56. <div id="c">  
  57. <p>C</p>  
  58. </div>  
  59. </div>  
  60. </div>  
  61. <div id="cursor">  
  62.  
  63. </div>  
  64. </body>  
  65. </html> 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 中江县| 富阳市| 封丘县| 兴仁县| 天峨县| 陈巴尔虎旗| 昌吉市| 米易县| 阿克| 永丰县| 文昌市| 琼结县| 永安市| 临海市| 舒兰市| 广昌县| 准格尔旗| 海原县| 即墨市| 望谟县| 阿勒泰市| 遂昌县| 宁德市| 泰安市| 那曲县| 明光市| 和田县| 松滋市| 大冶市| 长岛县| 聊城市| 繁峙县| 临泽县| 石屏县| 额济纳旗| 临洮县| 台山市| 镶黄旗| 西峡县| 达州市| 越西县|