国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表

圖片精選

主站蜘蛛池模板: 灌南县| 永康市| 津市市| 平凉市| 临颍县| 石门县| 肥乡县| 兴业县| 乌拉特中旗| 崇阳县| 赤峰市| 武宁县| 光泽县| 博兴县| 太湖县| 大邑县| 陆川县| 东安县| 修武县| 平山县| 吴忠市| 遵义县| 叙永县| 清河县| 琼海市| 浦县| 勃利县| 安宁市| 伊吾县| 柳河县| 堆龙德庆县| 灯塔市| 平阳县| 大余县| 正蓝旗| 兴宁市| 深泽县| 大兴区| 闻喜县| 靖江市| 慈利县|