2011-05-18 93 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一个整数值。

现在参数在正确的上下文中。

相关问题