javascript - Remove class using index - Stack Overflow

This might be easy but I am having trouble in getting it to work. I am using .each() to iterate through

This might be easy but I am having trouble in getting it to work. I am using .each() to iterate through a list. I was wondering if it is possible to remove a class using the index.

eg. If there were 10 items in the list and I want to remove the class from the 5th element when I click the 8th element for example.

$(function () {
var items = $('#v-nav>ul>li').each(function (index) {
    $(this).click(function () {
        if (index = 8)
{
$(#5).removeClass('class');
}
    });
});

Anyone with any ideas? Thank you

This might be easy but I am having trouble in getting it to work. I am using .each() to iterate through a list. I was wondering if it is possible to remove a class using the index.

eg. If there were 10 items in the list and I want to remove the class from the 5th element when I click the 8th element for example.

$(function () {
var items = $('#v-nav>ul>li').each(function (index) {
    $(this).click(function () {
        if (index = 8)
{
$(#5).removeClass('class');
}
    });
});

Anyone with any ideas? Thank you

Share Improve this question asked Nov 30, 2012 at 16:06 everreadyeddyeverreadyeddy 7481 gold badge8 silver badges18 bronze badges 1
  • You don't need top iterate all the li elements, instead you can directly add an event handler on the targeted li. – The Alpha Commented Nov 30, 2012 at 16:45
Add a ment  | 

3 Answers 3

Reset to default 8

Change

$(#5).removeClass('class');

To

$('#v-nav>ul>li:eq(4)').removeClass('class');

Its better to assign the element returned by your selector to do the same processing again to get desired element.

$(function () {
    var items = $('#v-nav>ul>li')

       $('#v-nav>ul>li').click(function () {
          if ($(this).index() = 8)
          {
             $(items).eq(4).removeClass('class');
          }
       });
});

There's no need to iterate with each(), as each element already has an index, and using eq() will let you select elements based on the index they have in the DOM. This solution is dependant on the elements being siblings() etc.

$(function () {
    var elems = $('#v-nav>ul>li');

    elems.on('click', function() {
        if ($(this).index()==8) elems.eq(4).removeClass('class');
    });
});

You could also just bind the click to that one element:

$(function () {
    $('#v-nav>ul>li:eq(7)').on('click', function() {
         $(this).siblings().filter(':eq(4)').removeClass('class');
    });
});

Otherwise I would just do:

$(function () {
    var elems = $('#v-nav>ul>li');
    $.each(elems, function(idx, elm) {
        if (idx==8) elems.eq(4).removeClass('class');
    });
});

As a sidenote ID's consisting of just a number (or starting with a number) is invalid.

Actually you don't need to iterate over all the li elements, instead you can do it like this way

$(function(){
  $('#v-nav>ul>li').eq(7).on('click', function(){ // eq(7) is 8th li
    $(this).closest('ul').find('li').eq(4).removeClass('cls'); //eq(4) is 5th li
  });
});

DEMO.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744656433a4586209.html

相关推荐

  • javascript - Remove class using index - Stack Overflow

    This might be easy but I am having trouble in getting it to work. I am using .each() to iterate through

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>