简体   繁体   English

根据属性按类别选择元素,然后添加或删除类别

[英]Select elements by class based on attr then add or remove class

For one of my projects I want to store the data information of each element of my navigation menu when I click on a button. 对于我的一个项目,我想在单击按钮时存储导航菜单中每个元素的data信息。

I've built a one-page website with two levels of navigation. 我建立了一个包含两个导航级别的单页网站。

  • One main menu 一个主菜单
  • Button on the bottom of each "page" 每个“页面”底部的按钮

My problem is that I've written a script to add/remove the class "selected" when I use the main menu. 我的问题是,当我使用主菜单时,我编写了一个脚本来添加/删除“选定”类。 So everything is working fine so far BUT I want the menu to add/remove the class "selected" when I use the second level of navigation too. 因此到目前为止,一切工作正常,但是当我也使用第二级导航时,我希望菜单添加/删除“选定”类。 That's where I'm stuck at the moment. 那是我目前停留的地方。

Here's what I have: 这是我所拥有的:

My main menu: 我的主菜单:

<nav>
  <ul>
    <li><a class="btn selected" href="#accueil" data-scrollOptions="accueil">accueil</a></li>
    <li><a class="btn" href="#apropos" data-scrollOptions="apropos">à propos</a></li>
    <li><a class="btn" href="#nosprojets" data-scrollOptions="nosprojets">nos projets</a></li>
    <li><a class="btn" href="#contact" data-scrollOptions="contact">contact</a></li>
  </ul>
</nav> 

The button in the "page": “页面”中的按钮:

<span class="scrolldown"><a href="#apropos" data-scroll="apropos">ScrollDown</a></span>
<span class="scrolldown"><a href="#nosprojets" data-scroll="nosprojets">ScrollDown</a></span>
<span class="scrolldown"><a href="#contact" data-scroll="contact">ScrollDown</a></span>

My 1st script: (working) 我的第一个脚本:(正在运行)

// onClick - class distribution
$(".btn").click(function () {
  $(".btn").removeClass("selected");
  $(this).addClass("selected");        
});

My 2nd script: (not working) 我的第二个脚本:(不起作用)

// onClick - dataTrigger
$(".scrolldown").click(function() {
  $('.btn').each(function () {
    var myArray =[];
    var attributs = $('.btn').data('scroll-options');
    myArray.push(attributs);
    console.log(myArray);
  });
});

How can I push each value ( accueil , apropos , nosprojets , contact ) in my array and then use it to add/remove class? 如何将每个值( accueilaproposnosprojetscontact )推nosprojets数组中,然后使用它添加/删除类?

The result for now console.log of my array 现在我阵列的console.log的结果

$('.btn').each(function () {
    var myArray =[];
    var attributs = $('.btn').data('scroll-options');
    myArray.push(attributs);
    console.log(myArray);
  });

You might want to be using the 'this' context to resolve your problem. 您可能希望使用“ this”上下文来解决您的问题。 While you are in your 'each' loop, this line : 当您处于“每个”循环时,此行:

var attributs = $('.btn').data('data-scrollOptions');

won't select a single item. 不会选择一个项目。 In fact, in this case, it is probably returning the data value for the first $('.btn') item. 实际上,在这种情况下,它可能返回第一个$('。btn')项的数据值。

To access each value, you could do something like this : 要访问每个值,您可以执行以下操作:

var attribut = $(this).data('data-scrollOptions');

'this' refers to the current context. “这”是指当前上下文。 In our case, it is the scope of a single 'btn' item, in the each loop. 在我们的例子中,它是每个循环中单个“ btn”项的范围。 Hope this helps. 希望这可以帮助。

Maybe you're overcomplicating the way to do this. 也许您过于复杂了。 There's a bunch of different ways to do it, and a number of different events you could use (so don't anyone shoot me for not doing it "their preferred way"). 有很多不同的方法可以使用,并且可以使用许多不同的事件(因此,没有人会因为不按“他们的首选方法”而射击我)。 But you could do this instead. 但是您可以改为执行此操作。

$('.scrolldown').on('click', function(){

   $('.btn', 'nav').removeClass('selected');
   $('.btn[href="'+$('a',this).attr('href')+'"]','nav').addClass('selected'); 

});

 (function($) { $('.scrolldown').on('click', function() { $('.btn', 'nav').removeClass('selected'); $('.btn[href="' + $('a', this).attr('href') + '"]', 'nav').addClass('selected'); }); })(jQuery); 
 .selected { background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav> <ul> <li><a class="btn selected" href="#accueil" data-scrollOptions="accueil">accueil</a> </li> <li><a class="btn" href="#apropos" data-scrollOptions="apropos">à propos</a> </li> <li><a class="btn" href="#nosprojets" data-scrollOptions="nosprojets">nos projets</a> </li> <li><a class="btn" href="#contact" data-scrollOptions="contact">contact</a> </li> </ul> </nav> <div> <span class="scrolldown"><a href="#apropos" data-scroll="apropos">ScrollDown</a></span> <span class="scrolldown"><a href="#nosprojets" data-scroll="nosprojets">ScrollDown</a></span> <span class="scrolldown"><a href="#contact" data-scroll="contact">ScrollDown</a></span> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM