2010年6月5日土曜日

5. サンプル コード2

<pre> <next>
サンプルコード1と同様に、このコードもhtmlのファイルにしてSafariで実行してみてください。

実行すると灰色の四角が表示され、表示された灰色の四角をクリックするとクリックした位置のX座標と四角の左端の座標、そして謎の「undefined」がアラートボックスで表示されます。
  1. <html>  
  2. <head>  
  3. <style>  
  4. .myCSS {  
  5.   background-color: rgb(240, 240, 240);  
  6.   height: 250px;  
  7.   width: 200px;  
  8.   position: absolute;  
  9.   left: 200px;  
  10.   top: 200px;  
  11. }  
  12. </style>  
  13. <script>  
  14. function MyObject() {  
  15.  var self = this;  
  16.  var myObject = document.createElement('div');  
  17.  myObject.className = 'myCSS';  
  18.  myObject.addEventListener('mousedown'this.myHandler, false);  
  19.  this.myObject = myObject;  
  20.  document.body.appendChild(myObject);  
  21. }  
  22.   
  23. MyObject.prototype.myHandler = function(e) {  
  24.  alert(e.clientX);  
  25.  alert(this.offsetLeft);  
  26.  this.style.left = '100px';  
  27. }  
  28.   
  29. function loaded() {  
  30.  myRect = new MyObject;  
  31. }  
  32. </script>  
  33. </head>  
  34. <body onload="loaded()">  
  35. </body>  
  36. </html>  
<pre> <next>

0 件のコメント: