Javascript 使用引导轮播脚本无法读取未定义的属性“offsetWidth”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29027830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:46:13  来源:igfitidea点击:

Getting Cannot read property 'offsetWidth' of undefined with bootstrap carousel script

javascriptjquerytwitter-bootstrap

提问by Xeen

I created a carouselwith Bootstrap 3.3and it works on my local machine, but when I upload the whole thing on server where the bootstrap js file is being compiled together with other files in a single file, I get this error:

我创建了一个carouselwithBootstrap 3.3并且它可以在我的本地机器上运行,但是当我将整个东西上传到服务器上时,引导程序 js 文件正在与单个文件中的其他文件一起编译,我收到此错误:

Cannot read property 'offsetWidth' of undefined- has anybody faced this and are there any known solutions to this issue?

Cannot read property 'offsetWidth' of undefined- 有没有人遇到过这个问题,是否有任何已知的解决方案?

回答by Hyman James

For me it was because I hadn't set an activeclass on any of the slides.

对我来说,这是因为我没有active在任何幻灯片上设置课程。

回答by sunny

For me, I changed class='carousel-item'to class='item'like this

对我来说,我变成class='carousel-item'class='item'喜欢这个

<div class="item">
    <img class="img-responsive" src="..." alt="...">
</div>

回答by Anup Shetty

Remove the class 'Carousal slide' on page load & add it dynamically when image gets loaded using jquery.This fixed for me

在页面加载时删除“Carousal slide”类并在使用 jquery 加载图像时动态添加它。这对我来说是固定的

回答by dgncn

I got same error. Because i used v4 alpha class names like carousel-control-nextWhen i changed with v3, problem solved.

我有同样的错误。因为我使用了 v4 alpha 类名称,例如carousel-control-next当我使用 v3 更改时,问题解决了。

回答by Serhii Havryliuk

"Change to if (typeof $next == 'object' && $next.length) $next[0].offsetWidth" -did not help. if you convert Bootstrap 3 to php(for WordPress theme), when adding WP_Query ($loop = new WP_Query( $args );) insert $count = 0;. And and at the end before endwhile; add $count++;.

“更改为 if (typeof $next == 'object' && $next.length) $next[0].offsetWidth” - 没有帮助。如果将 Bootstrap 3 转换为 php(用于 WordPress 主题),则在添加 WP_Query ($loop = new WP_Query( $args);) 时插入 $count = 0;。并且在结束之前结束;添加 $count++;。

回答by Hina Azhar

I have got the same error, but in my case I wrote class names for carousel item as .carousel-item the bootstrap.css is referring .item. SO ERROR solved. carosel-item is renamed to item

我遇到了同样的错误,但在我的情况下,我将轮播项目的类名写为 .carousel-item bootstrap.css 指的是 .item。SO ERROR 解决了。carosel-item 重命名为 item

<div class="carousel-item active"></div>

RENAMED To the following:

重命名为以下内​​容:

<div class="item active"></div>