我的索引默认如下:
...
<html>
<head>
...
</head>
<body>
...
<script type="text/javascript" src="/Content/assets/script/script.js"></script>
...
<script src="/Content/assets/script/combined.js" type="text/javascript"></script>
...
</body>
</html>
我的script.js是这样的:
function swiperHomepage(swiperOpt) {
//console.log(swiperOpt)
if ($('body').hasClass('pagemode-edit')) {
var swiper = new Swiper(swiperOpt.className, {
...
});} else {
var swiper = new Swiper(swiperOpt.className, {
...
});
}
...
}
我的combined.js是这样的:
if (!$('body').hasClass('pagemode-edit')) {
console.log('works');
var slidercouter = 0;
$('.sec-homepage .swiper-slide').each(function () {
var lengthx = $.trim($(this).find('.swiper-slide-bg').html()).length;
if (lengthx == 0) {
$(this).hide();
slidercouter++;
}
if (slidercouter <= 1) {
$('.swiper-pagination').css('display', 'none');
}
});
}
var swiperOpt = {
className: $('.swiper-container.swiper-homepage'),
numberPerView: 1,
spaceBetween: 0
}
swiperHomepage(swiperOpt);
如果索引运行,则存在这样的错误:
Uncaught TypeError: Cannot read property '0' of undefined
at Swiper.updateSize (swiper.min.js:173)
at Swiper.update$$1 [as update] (swiper.min.js:444)
at animateContent (script.min.js?v=1.17:55)
at swiperHomepage (script.min.js?v=1.17:56)
at HTMLDocument.<anonymous> (combined.js?v=0.0.20:244)
at j (jquery-3.2.1.min.js:2)
at k (jquery-3.2.1.min.js:2)
错误HTMLDocument.<anonymous> (combined.js?v=0.0.20:244
在Combined.js中是swiperHomepage(swiperOpt);
似乎是问题所在,似乎无法在外部js文件之间调用函数
我该如何解决这个问题?