2011-05-18 89 views
0

我做使用JQuery和YouTube JS API視頻播放器,我VARIOS控制YouTube播放器對象的方法,例如:錯誤:調用NPObject上的方法時出錯!與YouTube JSAPI

 setVolumen: function (value) { 

     if (value < 0 || value > 100) { 
      this.throwError("Volumen inválido: "+ value); 
      return; 
     } 

     this.volume = value; 

     this.elements.player.each(function() { 
      if (typeof this.setVolume != "undefined") { 
       this.setVolume(value); 
      } 
     }); 
    }, 

「this.elements.player」是yt播放器對象的jquery選擇器。我從player-controll實例調用一個setVolume方法並工作。例如:

 // This is inside a method of controller class 
     // and self is a reference to "this" in method context 

     this.celements.volume.slider({ 
      max: 100, 
      value: 100,     
      change: function (event, obj) { 
       self.setVolumen(obj.value); 
      } 
     }); 

如果我移動滑塊元素,這個工程沒有問題,買我打電話從jQuery選擇的方法:

$( '#播放列表')UplayList( 'setVolumen',80) ;

this thrown「錯誤:在NPObject上調用方法時出錯!」

該對象存在,並且setVolume不是未定義的。我不明白。

jQuery的一個有:

$.fn.extend({ 

    UplayList: function (options) { 
     this.each(function () { 

      if ($(this).data('uplaylistId')) { 
       var instance = $.playList.instances[$(this).data('uplaylistId')]; 
       instance[options].apply(instance, Array.prototype.slice.call(arguments, 1)); 
      } else { 
       var instance = new $.playList(this, options); 
       var id = instance.elements.player.attr('id'); 
       $(this).data('uplaylistId',id); 
      } 

     }); 
    } 
}); 

回答

0

首先,sorrt我englesh,我這個平移,與谷歌平移,。

Youtube API或jQuery沒問題。

當我重新調用被調用的方法時,我分別在「this」,(jQuery選擇器),傳遞的選項的元素之間,不在正確的contenxt中。

我更改爲:

$.fn.extend({ 

    UplayList: function (method) { 

     if ($(this).data('uplaylistId')) { 
      var instance = $.playList.instances[$(this).data('uplaylistId')]; 
     } else { 
      var instance = new $.playList(this, method); 
      var id = instance.elements.player.attr('id'); 
      $(this).data('uplaylistId',id); 
      return $(this); 
     } 

     if (instance[method]) { 
      return instance[method].apply(instance, Array.prototype.slice.call(arguments, 1)); 
     } else { 
      $.error('Method ' + method + ' does not exist'); 
     } 

    } 
}); 

所屬varible 「參數」 的內容作爲第二元件,一個對象。所以我調用setVolume方法發送一個元素作爲參數,當這resresed一個整數值。

現在參數在正確的上下文中。

相關問題