【问题标题】:Error: Error calling method on NPObject! with Youtube jsapi错误:在 NPObject 上调用方法时出错!使用 Youtube jsapi
【发布时间】:2011-05-18 21:57:46
【问题描述】:

我正在使用 JQuery 和 YouTube JS API 制作视频播放器,我有多种方法来控制 youtube 播放器对象,例如:

     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 选择器调用一个方法:

$('#playlist').UplayList('setVolumen',80);

这抛出了“错误:在 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);
            }

        });
    }
});

【问题讨论】:

    标签: javascript jquery youtube


    【解决方案1】:

    我没有遇到 Youtube API 或 jQuery 的问题。

    当我收到调用的方法时,在“this”的每个元素之间,(JQuery 选择器)验证传递的选项的上下文。

    我把它改成了:

    $.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' );
            }
    
        }
    });
    

    作为第二个元素的变量“参数”内容是一个对象。所以当我调用 setVolume 方法发送一个元素作为参数时,它接收到一个整数值。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 2014-01-25
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多