【问题标题】:Upon click, how to select all given elements of a given class that were not selected?单击后,如何选择给定类的所有未选择的给定元素?
【发布时间】:2019-10-18 17:21:53
【问题描述】:

假设您有一个包含项目的列表

<ul class = "tabs">
  <li class = "tab tab1 active">
  <li class = "tab tab2">
  <li class = "tab tab3">
</ul>

这用于在您拥有的 div 中显示内容

<div class = "tabs">
   <div class = "tab tab1 active">
   <div class = "tab tab2">
   <div class = "tab tab3">
</div>

我试图弄清楚如何在ul 列表中选择一个选项卡,使其div 设置为active 和以前的active divul 有它们的active 类删除。

有没有等价的

$(li. has tab in class name).addClass('active' if not already);
$(li. has tab in class name but the ones I didn't select).removeClass('active');
lis = $(li.tab);
$(div.has tab in class name).each(set class same as corresponding li);

或者我应该以某种方式使用id 来处理这类事情吗?

真的很好奇!这样做的专业方法是什么?

谢谢!

编辑:

似乎有这样的工作:

$('li.tab').on('click', function() {
    var ind = $(this).index();
    $(this).addClass('active').siblings().removeClass('active'); 
 $('div.tab').eq(ind).addClass('active').siblings().removeClass('active');
});

在这里看到了那个方法:Jquery addclass/removeclass on click

【问题讨论】:

  • 获取所有.tabs,将.active从所有.active中删除,添加到选定的一个(.index() + .eq()
  • 感谢您的建议。不确定如何实现,但找到了这个解决方案 $(this).addClass('active').siblings().removeClass('active');这是我在这里看到的工作:stackoverflow.com/questions/13295982/…
  • 您不需要任何 jQuery。请参阅下面的答案,它已经完全正常工作,只需要样式。
  • 好的,试试看!我的类名有额外的类,所以我想我只需要修改,以便我使用某种包含语法的类。
  • 我回答的重点是表明您可能不需要大部分课程,也不需要 jQuery。

标签: jquery


【解决方案1】:

看看 HTML/JS 最好的地方,没有一种最好的方法。当然肯定有更糟糕的方法。但更好或最好真的取决于我们想要达到的目标。

根据我作为 Web 开发人员的经验,并查看了各种 jQuery 插件,通常开发人员都有一些原则。比如:

HTML 应该在结构上尽可能地定义,这样即使不阅读后面的 JS,开发人员也应该知道这个特定的功能或 UX 正在尝试或将要做什么。

我会这样做的方式如下,这又不是最好的方式。但我会强调这种方法的好处;

首先是标记

<ul class="tabs" data-tabable-target="#my-tabable-data">
    <li class="tab active"><a href="#shopping-tab">Shopping</a></li>
    <li class="tab"><a href="#books-tab">Books</a></li>
    <li class="tab"><a href="#movies-tab">Movies</a></li>
</ul>

<div class="panels" data-tabable="" id="my-tabable-data">
    <div id="shopping-tab">1</div>
    <div id="books-tab">2</div>
    <div id="movies-tab">3</div>
</div>

我使用data-tabable 属性的原因是,我更喜欢样式类。你也可以添加一个类名tabable,比如&lt;div class="panel tabable"&gt;,然后添加你的jQuery代码来实现你的功能,同时通过类来定位你的样式。

然后像这样写我的jQuery:

$('[data-tabable-target]').each(function() {
    target = $(this.data('tabable-target'));
    target.find('div').addClass('hidden');
    this.children('li').click(function() {
        tabToBeActivated = $(this.children('a').get(0).href);
        tabToBeActivated.removeClass('hidden');
    });
});
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

这实质上将使我的代码可重用,这允许我在同一页面中添加单独的选项卡。类似的东西。

<ul class="tabs" data-tabable-target="#my-other-tabable-data">
    <li class="tab active"><a href="#shopping-tab">Shopping</a></li>
    <li class="tab"><a href="#books-tab">Books</a></li>
    <li class="tab"><a href="#movies-tab">Movies</a></li>
</ul>
<div class="panels" data-tabable="" id="my-other-tabable-data">
    <div id="hobbies-tab">1</div>
    <div id="travels-tab">2</div>
    <div id="random-tab">3</div>
</div>

真的,这取决于您的工作方式。试着解决眼前的问题。如果它是什么东西,我不会在一个页面中使用一次,而只是完成它。我可能会写你写的东西。如果它正在开发中,并且立即需要,我会使用 jQuery 插件。

你应该检查https://getbootstrap.com/docs/3.3/javascript/#tabs

【讨论】:

    【解决方案2】:

    假设两者在其父级中具有相同的索引位置。

    最小但有效的示例:

    const tabs = Array.from(document.querySelectorAll('.tabs li'));
    const panels = Array.from(document.querySelectorAll('.panels>div'));
    
    for (const [tabIndex, tab] of tabs.entries()) {
      tab.addEventListener('click', () => {
        for (const [panelIndex, panel] of panels.entries()) {
          panel.hidden = tabIndex !== panelIndex;
        }
        for (const xtab of tabs) {
          xtab.classList[xtab===tab ? 'add' : 'remove']('active');
        }
      })
    }
    .tabs .active {
      background-color: green;
      color: white;
    }
    
    .tabs li {
      cursor: pointer;
      background-color: #f2f2f2;
      list-style: none;
      margin-right: 2px;
      flex-grow: 1;
      text-align: center;
    }
    
    .tabs { display: flex; margin: 0; padding: 0; justify-content: stretch; }
    <ul class="tabs">
      <li class="active">1
      <li>2
      <li>3
    </ul>
    
    <div class="panels">
      <div>Panel 1</div>
      <div hidden>Panel 2</div>
      <div hidden>Panel 3</div>
    </div>

    【讨论】:

    • 我不知道如何处理我的类名中有多个类的事实。我编辑的帖子中的方法似乎有效,但可能会增加一些效率低下...
    • 由于第一个选项卡最初是活动的,我是否需要设置
    • 1 和
      1
  • 不,只需从第一个 div 中删除 hidden。如果您想要.active 按钮上的样式,请先将其放在相应的li 上。相应地编辑了示例。
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签