0

私はかなり基本的なjs関数をクラスに適応させています。とにかく、基本的にはメインページの上にフローティングコンテナを作成するだけです。不完全であることは承知していますが、入力中です。close()関数を呼び出そうとすると、問題が発生し続けます。this.sdivFirefoxは未定義を返します。close()がPopのメソッドであり、sdivがPopクラスの最初の行で定義されている場合にこれがどのように発生するかについて混乱していますか?

function Pop( wpx,hpx ){

  Pop.prototype.sdiv;
  Pop.prototype.shadow;
  Pop.prototype.pdiv;

  // start with the invisible screen, which
  // covers the main page
  this.sdiv = document.createElement("DIV")
  this.sdiv.className = "popScreen";
  this.sdiv.id = "popScreen";
  // this screen covers the full document, so 
  // base dimensions on the document size
  this.sdiv.style.width = document.body.clientWidth + "px";
  this.sdiv.style.height = document.body.clientHeight + "px"; 
  document.body.appendChild( this.sdiv );

  // attach drop shadow
  this.shadow = document.createElement("DIV");
  this.shadow.className = "popShadow";
  this.shadow.style.width = wpx + "px";
  this.shadow.style.height = hpx + "px";
  this.shadow.style.left = ( ( window.innerWidth / 2 ) - ( wpx / 2 )  ) + "px";
  this.shadow.style.top = ( ( window.innerHeight / 2 ) - ( hpx / 2 ) ) + "px";
  document.body.appendChild( this.shadow );

  this.pdiv = document.createElement("DIV");
  this.pdiv.className = "pop";
  this.pdiv.id = "pop";
  this.pdiv.style.position = "absolute";
  this.pdiv.style.width = wpx + "px";
  this.pdiv.style.height = hpx + "px";
  this.shadow.appendChild( this.pdiv );

  // bind an event to the screen div so that when it is clicked
  // the Pop dialogue is closed and the user is return to the main page
  $("div#popScreen").click( function( ){
    Pop.prototype.close( );
  } );

  Pop.prototype.go = function( url, method, data ){ 
    if( method == null )
      $("div#pop").load( url );
  }

  Pop.prototype.close = function( ){
      this.sdiv.parentNode.removeChild( this.sdiv );
      this.shadow.parentNode.removeChild( this.shadow );
      this.pdiv.parentNode.removeChild( this.pdiv );
  }

} 

助けてくれてありがとう

4

2 に答える 2

3

を使用してすべてのインスタンスPop.prototype.close()を閉じることはできません。代わりに、オペレータで作成した のPopインスタンスごとに、を呼び出す必要があります。PopnewpopInstance.close()

于 2009-01-20T11:48:49.657 に答える
1

thisjavascriptでは、oo 言語ではこれとはまったく異なる動作をします。

あなたが持っているエラーはここにあります:

  Pop.prototype.close = function( ){
      this.sdiv.parentNode.removeChild( this.sdiv );
      this.shadow.parentNode.removeChild( this.shadow );
      this.pdiv.parentNode.removeChild( this.pdiv );
  }

その関数thisでは、おそらくウィンドウを参照しています(ブレークポイントを設定し、firebug を調べてください)。

おそらく、これらの線に沿った何かが機能するでしょう。

 var parent = this;
 Pop.prototype.close = function(){
      parent.sdiv.parentNode.removeChild( this.sdiv );
      parent.shadow.parentNode.removeChild( this.shadow );
      parent.pdiv.parentNode.removeChild( this.pdiv );
  }
于 2009-01-20T11:55:33.917 に答える