jQuery 获取“无法读取未定义引导程序的属性”偏移量“

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30262999/
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-26 16:40:22  来源:igfitidea点击:

Getting 'cannot read property 'offsetwidth' of undefined bootstrap

jquerytwitter-bootstrap

提问by Pallavi Manohar

I get the offsetwidthundefined in spite of adding active class and also defining my jquery just above my js function. I have to use carousel for my work and I tried a mock one for practice. My first active image is displaying and after I click next I get this error in console and the images are not sliding.

offsetwidth尽管添加了活动类并在我的 js 函数上方定义了我的 jquery,但我还是得到了undefined 。我必须在我的工作中使用轮播,我尝试了一个模拟的来练习。我的第一个活动图像正在显示,单击下一步后,我在控制台中收到此错误并且图像没有滑动。

  <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">

  <title>mycooking</title>

</head>
<body>
  <!--indicatiors-->
  <div class="container">
    <div class="row-fluid">
      <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
          <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
          <li data-target="#myCarousel" data-slide-to="1"></li>
          <li data-target="#myCarousel" data-slide-to="2"></li>
        </ol>
        <!--wrapper for slides-->
        <div class="carousel-inner" role="listbox">
          <div class="item-active">
            <img class="first-slide" src="/img/dosa.jpg" alt="dosa" />
          </div>
          <div class="item">
            <img class="second-slide" src="/img/idli.jpg" alt="idli" />
          </div>
          <div class="item">
            <img class="third-slide" src="img/vada.jpg" alt="vada" />
          </div>
        </div>
        <!--Left and right controls-->
        <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
          <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
          <span class="sr-only">Previous</span>
        </a>
        <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
          <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
      </div>
    </div>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" type="text/javascript"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

  <script type="text/javascript">
    jQuery(document).ready(function () {
      $("#myCarousel").carousel({
          interval: 1200
      })
    });
  </script>
</body>
</html>

回答by Ted

Change this line in your HTML:

更改 HTML 中的这一行:

<div class="item-active">

to

<div class="item active">

That hyphen shouldn't be there. If you need an item-activeclass, add it after those two, like <div class="item active item-active">. itemand activeare classes bootstrap uses to run the carousel. Without them on that first slide, you'll get that error.

那个连字符不应该在那里。如果你需要一个item-active类,在这两个之后添加它,比如<div class="item active item-active">. item并且active是引导程序用来运行轮播的类。如果第一张幻灯片上没有它们,您将收到该错误。

HTH, -Ted

HTH, -Ted