jQuery localScroll - how to add class to clicked <

2019-08-22 13:56发布

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

标签: jquery scroll
3条回答
等我变得足够好
2楼-- · 2019-08-22 14:32

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>
查看更多
贪生不怕死
3楼-- · 2019-08-22 14:47

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

查看更多
在下西门庆
4楼-- · 2019-08-22 14:54

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");
});
查看更多
登录 后发表回答