手风琴ui.newHeader.index()增加2而不是1

时间:2018-10-02 16:31:53

标签: javascript jquery jquery-ui

我想问为什么jquery ui,ui.newHeader.index()每次增加2倍?

$("#accordion").accordion({

  collapsible: false,
  clearStyle: true,
  active: -1,
  heightStyle: "content",
  activate: function(event, ui) {
    event.stopPropagation();
    var activeIndexSearch = $("#search-ad").accordion("option", "active");
    newIndex = ui.newHeader.index()
    oldIndex = ui.oldHeader.index()
    alert("newIndex:" + newIndex)
    alert("oldIndex:" + oldIndex)
  }
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="accordion">
  <h3>Section 1</h3>
  <div>
    <p>
      Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum
      a velit eu ante scelerisque vulputate.
    </p>
  </div>
  <h3>Section 2</h3>
  <div>
    <p>
      Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna.
    </p>
  </div>
  <h3>Section 3</h3>
  <div>
    <p>
      Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam
      nisi, eu iaculis leo purus venenatis dui.
    </p>
    <ul>
      <li>List item one</li>
      <li>List item two</li>
      <li>List item three</li>
    </ul>
  </div>
  <h3>Section 4</h3>
  <div>
    <p>
      Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est.
    </p>
    <p>
      Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
    </p>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

检查jQuery .index()的规格:

  

如果未将任何参数传递给.index()方法,则返回值是一个整数,指示第一个元素在jQuery对象中的位置相对于其同级元素


因为每个标题标签之间都有<div>标签,所以标题的索引之间用1分隔。

<h3>Section 1</h3>   <!-- 0 -->
<div>...</div>       <!-- 1 -->

<h3>Section 2</h3>   <!-- 2 -->
<div>...</div>       <!-- 3 -->

<h3>Section 3</h3>   <!-- 4 -->
<div>...</div>       <!-- 5 -->

<h3>Section 4</h3>   <!-- 6 -->
<div>...</div>       <!-- 7 -->

如果您希望它们增加1,只需替换

newIndex = ui.newHeader.index();

使用

newIndex = ui.newHeader.index();
newIndex /= 2;

或者您也可以只考虑分离。你的电话。