如何在鼠标悬停时获取选定的索引? [英] how to get selected index on mouseover?

查看:104
本文介绍了如何在鼠标悬停时获取选定的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您告诉我如何将鼠标悬停在选定项索引上..我有一个列表,当我将鼠标悬停在列表上时,我添加了悬停类.但是我需要选定的项.

could you please tell me how I will get selected Item index on mouseover ..I have a list , when I mouseover the list I added hover class .But I need the selected Item .

这是我的代码

https://jsfiddle.net/qp6ex1jh/

 $(function(){
 $('#main-menu').on({
                mouseenter: function () {
                console.log("mouse over")    
                },
                mouseleave: function () {
                  console.log("mouse leave")    
                }
            });

 })

当我将鼠标悬停在 B 上时,它会给我1;当我将鼠标悬停在c上时,它会给我2

when I hover on B it give me 1 and when I hover c it give me 2

推荐答案

使用 index() 并将选择器移至<li>

$('#main-menu li').on({
    mouseenter: function() {
      console.log("over index " + $(this).index())
    },
    mouseleave: function() {
      console.log("leave index " + $(this).index())
    }
});

这篇关于如何在鼠标悬停时获取选定的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆