javascript - 类型错误 : undefined is not an object - Safari & IE

标签 javascript iframe backbone.js safari

runAnimation: function(){
  var iframe = document.querySelector('.active iframe');
  window.frames[iframe.id].contentWindow.runAnimation();
},

上述代码在 Safari 中打印以下错误:

TypeError: undefined is not an object (evaluating 'window.frames[iframe.id].contentWindow.runAnimation')

我已经尝试过答案here , herehere ,基本上所有可能的解决方案。

另一个需要指出的有趣的事情是 window 仅在 Safari 上返回 undefined!

/**
 * @property {array} Templates.slide - The template part for each slide
 * Html template for app
 */

//Iframe Template
presenter.Templates.slide = [
  '<iframe id="slide-<%= id %>" class="slide" data-disable-back="<%= disableBack %>" data-disable-forward="<%= disableForward %>" data-position="<%= position %>" data-src="<%= url %>">',
].join('\n');




/**
 * Default options for the carousel.
 * @public
 */

//Owl Carousel options
	Owl.Defaults = {
		...
		// Classes and Names
		themeClass: 'owl-theme',
		baseClass: 'owl-carousel',
		itemClass: 'owl-item',
		centerClass: 'center',
		activeClass: 'active'
	};

/**
 * The main view for the presentation
 * This renders a collection presentation
 * Builds owlCarousel and attaches api controls
 */
presenter.PresentationView = Backbone.View.extend({
  ...
  
  /**
   * Runs slide animation. Called on the slide itself once its loaded
   */
  runAnimation: function(){
    var iframe = document.querySelector('.active iframe');
    window.frames[iframe.id].contentWindow.runAnimation();
  },
  ...

/**
 * Adds listners for carousel events. Used to trigger runAnimation along with
 * other function
 */
  carouselEvents: function(){
    var self = this;
    this.slider.on('translated.owl.carousel', function(){
      self.showControls();
      self.runAnimation();
    });
  },
  
/**
 * Standard backbone method. Called when view is instantiated
 * @param {object} options - Options passed when creating the view
 *                           (such as instanceURL)
 */
  initialize : function(options){
    this.options = options;
    this.render();
    this.buildSlider();
    this.loadFour();
    this.carouselControls();
    this.carouselEvents();
    this.keyNavigation();
    this.UIEvent();
    this.reScale();
  },

});

最佳答案

我尝试过这样,似乎有效

var iframe = document.querySelector('.container iframe');
window.frames[iframe.id].contentWindow.runAnimation();

https://plnkr.co/edit/0pquvlK3beF1pqMcU1lc?p=preview

关于javascript - 类型错误 : undefined is not an object - Safari & IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36058565/

相关文章:

c# - 是否可以将js嵌入到c#中

javascript - 通过特定文本/使用 Jquery 检查项目列表

javascript - 刷新(不是重新加载)iFrame

javascript - 如何使下划线模板相互交互?

javascript - 主干 View 不刷新事件

javascript - 使用下划线的主干模板

javascript - 使用javascript更改html文档中所有类的名称

javascript - Firefox 中 javascript RegExp 的奇怪行为

css - 如何并排居中两个 iframe 直到它们达到最小宽度,然后将它们的宽度设置为 100%?

jquery - 使用jquery查找iframe中元素的高度