jQuery localScroll - how to add class to clicked <

2019-08-22 13:49发布

问题:

I got a scrolling content controlled via href's in navigation. I use localScroll to scroll the content. So question is how to add class to attribute of currently displayedpart of a content. That's how I initiate scroll

<script style="text/javascript">
        $(document).ready(function () {
        $('.navigation').localScroll();
        });
    </script>

Thanks, Arek

回答1:

I figured it out:

<script style="text/javascript">
        $(document).ready(function () {
            $('.navigation').localScroll();
            $('.navigation').find('a').click(selectNav);
            function selectNav() {
              $(this)
                .parents('ul:first') 
                  .find('a') 
                    .removeClass('selected') 
                  .end() 
                .end()
                .addClass('selected');
                }
                function trigger(data) {
                  var el = $('.navigation').find('a[href$="' + data.id + '"]').get(0);
                  selectNav.call(el);
                    }
        });
    </script>


回答2:

for that <a> do you have any id or you want to add class on click of every

if it is something like this <a id="target" > you can try the below

$('#target').click(function() {
  $("p").addClass("myClass yourClass");
});


回答3:

I was looking into this too, and the answer by @arekk gave me an idea. I implemented mine this way

$('#sub-menu a').click(function(){
    var links = $('#sub-menu a');
    links.removeClass('selected');
    $(this).addClass('selected');
});

I hope this works for anybody looking



标签: jquery scroll