使用索引在不同的div上添加Active Class

时间:2021-02-24 20:02:17

I have two divs having same class.

我有两个同一类的div。

Now I want when I click an anchor tag of any element. I want to add class on same position of second div as well.

现在我想点击任何元素的锚标签。我想在第二个div的相同位置添加类。

Mirror: JSFiddle

jQuery code:

$(document).on('click', '.loadDots li a', function () {
    $('.loadDots li a').removeClass('active');
    alert("clicked");
    $('.loadDots li a').eq($(this).index()).addClass('active');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="loadDots">
    <ul>
        <li><a ng-show="data.showIcon1" ng-click="getId(1)" class="active" href="#">aaaa</a>
        </li>
        <li><a ng-show="data.showIcon2" ng-click="getId(2)" href="#" class="">aaaa</a>
        </li>
        <li><a ng-show="data.showIcon3" ng-click="getId(3)" href="#" class="">aaaa</a>
        </li>
        <li><a ng-show="data.showIcon4" ng-click="getId(4)" href="#" class="">aaaa</a>
        </li>
        <li><a ng-show="data.showIcon5" ng-click="getId(5)" href="#" class="">aaaa</a>
        </li>
    </ul>
</div>
<div class="loadDots">
    <ul>
        <li><a ng-show="data.showIcon1" ng-click="getId(1)" href="#">ssss</a>
        </li>
        <li><a ng-show="data.showIcon2" ng-click="getId(2)" href="#" class="">sss</a>
        </li>
        <li><a ng-show="data.showIcon3" ng-click="getId(3)" href="#" class="">sss</a>
        </li>
        <li><a ng-show="data.showIcon4" ng-click="getId(4)" href="#" class="">sss</a>
        </li>
        <li><a ng-show="data.showIcon5" ng-click="getId(5)" href="#" class="">sss</a>
        </li>
    </ul>
</div>

Any ideas?

Thanks

1 个解决方案

#1


2  

Check the DEMO

检查演示

$(document).on('click','.loadDots li a',function() {
var that = $(this);
$('.loadDots li a').removeClass('active');
var ind = that.closest('li').index();
$('.loadDots').each(function() {
    $(this).find('li').eq(ind).find('a').addClass('active');
});

});

#1


2  

Check the DEMO

检查演示

$(document).on('click','.loadDots li a',function() {
var that = $(this);
$('.loadDots li a').removeClass('active');
var ind = that.closest('li').index();
$('.loadDots').each(function() {
    $(this).find('li').eq(ind).find('a').addClass('active');
});

});