Javascript 在生产服务器上未定义属性,但可用于开发
我们已经在wordpress主题中实现了一个lightbox,它在我们的开发服务器上运行得非常好,该服务器与我们的live site位于同一服务器上,据我所知,构建与开发站点的构建完全相同,因为开发站点的构建刚刚从live site备份中获取,但是由于某些页面的原因,我得到了以下错误,但在其他页面上,我并不是在这个JS文件之前加载JS,但我不明白为什么每次在dev服务器上它都能完美工作: 未捕获的TypeError:无法读取未定义的属性“imageUrl”Javascript 在生产服务器上未定义属性,但可用于开发,javascript,jquery,wordpress,Javascript,Jquery,Wordpress,我们已经在wordpress主题中实现了一个lightbox,它在我们的开发服务器上运行得非常好,该服务器与我们的live site位于同一服务器上,据我所知,构建与开发站点的构建完全相同,因为开发站点的构建刚刚从live site备份中获取,但是由于某些页面的原因,我得到了以下错误,但在其他页面上,我并不是在这个JS文件之前加载JS,但我不明白为什么每次在dev服务器上它都能完美工作: 未捕获的TypeError:无法读取未定义的属性“imageUrl” jQuery(文档).ready(函数
jQuery(文档).ready(函数($){
变量WHLightbox={
设置:{
覆盖:$('.portfolio tile--overlay'),
imageCell:$('.cell-image,.portfolio-tile-image')
},
数据:{
图片:[]
},
init:function(){
此为.events();
这是buildImageData();
},
事件:函数(){
var self=这个;
this.settings.imageCell.on('点击',函数(e){
e、 预防默认值();
e、 停止传播();
//设置覆盖层
//self._positionOverlay();
self._openOverlay();
self._防止滚动();
//创建图像幻灯片
self._createImageSlide($(此));
});
$('.portfolio tile--overlay--close')。打开('单击tap',函数(e){
e、 预防默认值();
e、 停止传播();
self._closeOverlay();
});
$('.portfolio tile--overlay--controls--prev,.portfolio tile--overlay--controls--next')。打开('click tap',函数(e){
e、 预防默认值();
e、 停止传播();
});
$('.portfolio tile--overlay--controls--prev')。打开('单击tap',函数(e){
e、 预防默认值();
e、 停止传播();
self.showPrev();
});
$('.portfolio tile--overlay--controls--next,.portfolio tile--overlay')。打开('click tap',函数(e){
e、 预防默认值();
e、 停止传播();
self.showNext();
});
},
//公共职能
showPrev:function(){
var index=this.currentImageIndex();
如果(索引==0){
索引=this.data.images.length;
}
此._createImageSlide(假,索引-1);
},
showNext:函数(){
var index=this.currentImageIndex();
if(index==this.data.images.length-1){
//设置为-1,因为它在_createImageSlide调用中添加了1
指数=-1;
}
此._createImageSlide(false,索引+1);
},
currentImageIndex:函数(){
if(this.settings.overlay.hasClass('open')){
var imageUrl=$('.portfolio tile--main image').attr('src');
对于(var i=0;iat它给出了错误的代码行?错误发生在第109行:imagePath=this.data.images[index].imageUrl;在本例中,确定元素this.data.images[index]
在实际索引中不存在。您应该调试它,看看数组中有多少元素this.data.images
,然后找到为什么索引>this.data.images.lenght
,但我不明白为什么它在开发服务器上工作得很好?它只是在不同的URL上的一个完全相同的元素,而且它也不是真正的你能感觉到它在某些页面上有效,但不是在所有页面上都有效吗?
jQuery(document).ready(function( $ ) {
var WHLightbox = {
settings: {
overlay: $('.portfolio-tile--overlay'),
imageCell: $('.cell-image, .portfolio-tile--image')
},
data: {
images: []
},
init: function() {
this.events();
this.buildImageData();
},
events: function() {
var self = this;
this.settings.imageCell.on('click tap', function(e) {
e.preventDefault();
e.stopPropagation();
// set up the overlay
//self._positionOverlay();
self._openOverlay();
self._preventScrolling();
// create the image slide
self._createImageSlide($(this));
});
$('.portfolio-tile--overlay--close').on('click tap', function(e) {
e.preventDefault();
e.stopPropagation();
self._closeOverlay();
});
$('.portfolio-tile--overlay--controls--prev, .portfolio-tile--overlay--controls--next').on('click tap', function(e) {
e.preventDefault();
e.stopPropagation();
});
$('.portfolio-tile--overlay--controls--prev').on('click tap', function(e) {
e.preventDefault();
e.stopPropagation();
self.showPrev();
});
$('.portfolio-tile--overlay--controls--next, .portfolio-tile--overlay').on('click tap', function(e) {
e.preventDefault();
e.stopPropagation();
self.showNext();
});
},
// public functions
showPrev: function() {
var index = this.currentImageIndex();
if(index === 0) {
index = this.data.images.length;
}
this._createImageSlide(false, index-1);
},
showNext: function() {
var index = this.currentImageIndex();
if(index === this.data.images.length-1) {
// set to -1 because it adds 1 in the _createImageSlide call
index = -1;
}
this._createImageSlide(false, index+1);
},
currentImageIndex: function() {
if(this.settings.overlay.hasClass('open')) {
var imageUrl = $('.portfolio-tile--main-image').attr('src');
for(var i=0; i<this.data.images.length; i++) {
if(this.data.images[i].imageUrl === imageUrl) {
return i;
}
}
} else {
return false;
}
},
// image data
buildImageData: function() {
var self = this,
i = 0;
this.settings.imageCell.each(function() {
self.data.images[i] = {
imageUrl: self._getImagePath($(this))
}
i++;
});
},
// slide
_createImageSlide: function($el, index) {
var imagePath;
if(!$el) {
imagePath = this.data.images[index].imageUrl;
} else {
imagePath = this._getImagePath($el);
}
this.settings.overlay.find('.portfolio-tile--main-image').attr('src', imagePath);
},
_getImagePath: function($el) {
var imagePath,
spanEl = $el.find('span.js-cell-image-background'),
imgEl = $el.find('img.cell-image__image');
if(spanEl.length) {
imagePath = spanEl.css('backgroundImage');
imagePath = imagePath.replace(/url\(["]*/,'').replace(/["]*\)/,'');
} else if(imgEl.length) {
imagePath = imgEl.attr('src');
}
return imagePath;
},
// overlay
//_positionOverlay: function() {
// this.settings.overlay.css({
// position the overlay to current scroll position
// top: $(window).scrollTop()
// });
//},
_openOverlay: function() {
this.settings.overlay.addClass('open');
},
_preventScrolling: function() {
$('html, body').addClass('no-scroll');
},
_reInitScrolling: function() {
$('html, body').removeClass('no-scroll');
},
_closeOverlay: function() {
this.settings.overlay.removeClass('open');
this._reInitScrolling();
}
};
WHLightbox.init();
});