Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $(此).index()选项?_Javascript_Jquery_Html - Fatal编程技术网

Javascript $(此).index()选项?

Javascript $(此).index()选项?,javascript,jquery,html,Javascript,Jquery,Html,我有以下选项卡功能: //Tabs /// $('.tabs').each(function() { var $tabs = $(this); $tabs.find('.section-body > div:not(:first-child)').hide().end() .find('.section-head a:eq(0)').addClass('active').end() .find('.section-head a').on('click', function()

我有以下选项卡功能:

//Tabs ///
$('.tabs').each(function() {
  var $tabs = $(this);
  $tabs.find('.section-body > div:not(:first-child)').hide().end()
 .find('.section-head a:eq(0)').addClass('active').end()
 .find('.section-head a').on('click', function() {

    $tabs.find('.section-head a').removeClass('active').end()

  .find('.section-body > div:eq(' + $(this).index() + ')').show().siblings().hide();
    $(this).addClass('active');
  });
});
它可以工作,但是$this.index与另一个插件冲突,当$this.index存在时,另一个插件停止工作。是否有其他方法可以在不使用$this.index的情况下获得相同的制表符结果

以下是HTML(如果需要):

<div class="section tabs">
  <div class="section-head">
    <a>Section A</a>
    <a>Section B</a>
  </div>
  <div class="section-body">
    <div>
      <p>Section A</p>
    </div>
    <div>
      <p>Section B</p>
    </div>
  </div>
</div>
这里有一个JS提琴:

您有两个解决方案

首先,不应该有任何歧义。因此,更改插件的名称。这很容易。 其次,如果您不想执行第一步,请按照以下说明操作: 在包含插件之前,请执行以下操作:

$.fn.myIndex = $.fn.index;
上面所做的是,它创建了一个新的插件myIndex,该插件传递了index的函数引用
现在,您可以使用$this.myIndex来提供与$this.index相同的效果,但不会产生歧义

您可以而且应该使用传递给的参数。每个参数:

.每个功能

function
Type: Function( Integer index, Element element )
A function to execute for each matched element
因此,它将成为:

$('.tabs').each(function(idx, elt) {
  var $tabs = $(elt);// $(elt) is equivalent to $(this)
  $tabs.find('.section-body > div:not(:first-child)').hide().end().find('.section-head a:eq(0)').addClass('active').end().find('.section-head a').on('click', function() {
    $tabs.find('.section-head a').removeClass('active').end().find('.section-body > div:eq(' + idx + ')').show().siblings().hide();
    $(elt).addClass('active');
  });
});

您不能简单地使用jQuery符号吗

jQuery(this).index();
或者如果真的需要,将jquery置于noConflict模式

jQuery.noConflict();
现在$control返回到其他库。请参阅此处的更多信息:

您还可以从您的数据库中获取索引。如果您将第一个参数传递给函数eachfunction index,elem{}在您最复杂的示例中,我发现很难判断$this在事件处理程序中指的是什么…通过使用链接和ID正确关联导航和选项卡,然后选择link.attr'href'。您能告诉我如何在JSFIDLE中使用第二个选项吗?我以前从未见过这种情况请看这里:检查元素,你能看到错误吗?