简体   繁体   English

Javascript-如何突出显示最后单击的列表项

[英]Javascript - How to highlight the last clicked list item

I have a sidebar list of items in HTML on a bootstrap page and i would like the last clicked item to be highlighted with a "active" class. 我在引导页面上有HTML项的侧边栏列表,我希望最后单击的项以“活动”类突出显示。 I was wondering how i can get the whole list (ul) like an array so i could highlight a certain item (li) or highlight the last clicked item. 我想知道如何像数组一样获取整个列表(ul),以便突出显示某个项目(li)或突出显示最后单击的项目。

The list is structured like this: 该列表的结构如下:

<div class="container-fluid">
  <div class="row">
    <div class="col-sm-3 col-md-2 sidebar sidebar-style">
      <ul class="nav nav-sidebar sidebar-scrollable">
        <li class="active"><a href="#" onclick="jump(0)">Home</a></li>
        <li><a href="#" onclick="jump(1)">Site 001</a></li>
        <li><a href="#" onclick="jump(2)">Site 002</a></li>
        <li><a href="#" onclick="jump(3)">Site 003</a></li>
        <li><a href="#" onclick="jump(4)">Site 004</a></li>
        <li><a href="#" onclick="jump(5)">Site 005</a></li>

What i need is some JS code which can set the last clicked li item to class="active" but still have the option to set any of the li items to the active one, for example if i wanted to have a random button and it selected item 4 then 2 etc. I guess the main thing i need is a way to have all list items like an array. 我需要的是一些JS代码,可以将最后单击的li项设置为class =“ active”,但仍然可以选择将任何li项设置为active项,例如,如果我想要一个随机按钮,选择项目4然后2等。我想我主要需要的是一种使所有列表项都像数组一样的方法。

I strongly suggest you use jQuery which is kind of simulating DOM manipulation like an array or more precisely an object. 我强烈建议您使用jQuery,它是一种类似于数组或对象的DOM模拟DOM操作。

My suggestion would be the following: 我的建议如下:

$(".nav-sidebar").on('click', 'li', function(e) {
    $(this).parent().find('li.active').removeClass('active');
    $(this).addClass('active');
});

This code is removing the active class from all li items, and adding active class to currently clicked li item. 此代码是从所有li项目中删除活动类,并将活动类添加到当前单击的li项目中。

Cheers, 干杯,

Edit PS: 编辑PS:

You could also bind your event directly like this: 您还可以像这样直接绑定事件:

$(".nav-sidebar li").click(...);

But the purpose of using .on() is that it will bind the event dynamically, so that if you decide to add a new li to your $(".nav-sidebar") element, it will also trigger the event on that element. 但是,使用.on()的目的是它将动态绑定事件,因此,如果您决定向$(".nav-sidebar")元素添加新li,它也会触发该元素上的事件。

Edit: 编辑:

To answer @zeddex your question: How to select manually li 4? 要回答@zeddex,您的问题是:如何手动选择li 4? You simply use the following: 您只需使用以下内容:

$("li:eq(3)").trigger('click');

That'll manually trigger a click event for li 4. If you want to select a specific li you can use a method of the like: :eq(x) in which x is the position of the li item you want to reach starting from 0 which corresponds to your first li . 这将手动触发li 4的click事件。如果要选择特定的li,则可以使用以下方法:eq(x) ,其中x是您要从以下位置到达li项目的位置0对应于您的第一个li Have a look at jQuery DOC on that: jQuery DOC on :eq() 看一下jQuery DOC ::eq()上的jQuery DOC。

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

相关问题 Vuejs 突出显示项目列表中单击项目的颜色? - Vuejs highlight color on a clicked item in a list of items? Vanilla Javascript 如何检查单击的元素是否是最后一项 - Vanilla Javascript how to check if clicked element is last item 如何突出显示 JS fetch() 中点击的项目 - How to highlight the clicked item from JS fetch() 如何在使用JavaScript的会话中存储单击列表项? - How do I store a clicked list item in a session with JavaScript? 如何突出显示单击的图像并在列表中显示其名称 - How to highlight the clicked image and show the name of it in a list 单击任何其他项目时如何将(最后单击的列表项目)恢复为原始颜色 - React hooks - how to revert (last clicked list item) back to its original color when any other item is clicked - react hooks 如何在 javascript 中检查项目是否被点击 - how to check if an item is clicked or not in javascript 当使用Jquery / JavaScript单击上一个列表项时,如何定位嵌套在列表项内的元素? - How do I target an element nested inside a list item when the previous list item is clicked with Jquery/JavaScript? 单击列表项时未调用Javascript函数 - Javascript function is not called when the list item is clicked 确定列表中的哪个项目被点击-javascript - determining which item of a list was clicked- javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM