javascript - 为第一项元素添加事件类

标签 javascript jquery twitter-bootstrap

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
  
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
	<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img src="http://ichef.bbci.co.uk/news/976/cpsprodpb/87D3/production/_84817743_beckhamfashion.jpg" alt="...">
      <div class="carousel-caption">
        ...
      </div>
    </div>
    <div class="item">
      <img src="http://ichef.bbci.co.uk/news/976/cpsprodpb/9949/production/_84814293_dummy.jpg" alt="...">
      <div class="carousel-caption">
        ...
      </div>
    </div>
    ...
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" 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="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
  
  
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

例如,我正在使用 bootstap carousel;

<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">

  <div class="carousel-inner" role="listbox">

    <div class="item active">
      <img src="..." alt="...">
      <div class="carousel-caption">

      </div>
    </div>

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

  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" 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="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

正如您所看到的,我的第一个 .item 类具有 .active 类,但我想在删除 .active 类时自动添加它来自 .item 类,然后 javascript 给了我这个错误: TypeError: Cannot read property 'offsetWidth' of undefined/works/anitur/js/bootstrap.min.js:6 和我尝试使用此代码添加

$(".carousel > .item:first").addClass("active");

但是什么都没有改变我怎么能实现我想要的呢?顺便问一下,有没有办法用js而不是html添加轮播控件?

最佳答案

最好的做法是在 HTML 模板本身中添加轮播类。为什么是因为轮播插件会在文档准备好后重建 HTML。不过您可以尝试下面的代码。

$(".carousel .item").first().addClass("active");

关于javascript - 为第一项元素添加事件类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41713070/

相关文章:

jQuery -parent() 不在回调函数中工作

javascript - 如何在外部 javascript 函数中制作 jquery load()?

javascript - HTML代码转PNG图片

twitter-bootstrap - 如何在 Bootstrap 中使内容接触视口(viewport)的边缘

javascript - jquery 选择器 ie8 问题

javascript - Zendesk `app.js` 使用完整 URL 调用外部 API?

javascript - 如何使用左右渲染的一堆图 block 来渲染无缝平铺 Sprite

Javascript/NodeJs 优化重复的 if-else

javascript - 有人可以解释 Bootstrap 4 Button Javascript 的 jQuery/JQUERY_NO_CONFLICT 特定代码吗?

ruby-on-rails - erb 表单上 Bootstrap 工具提示的语法?