get index of current hovered li respect to visible li only - jquery hover
There are 5 li element and one of them say third li is hidden.Is there a way to apply filter before getting index so that fourth element's index should show 2 instead of 3.
<html>
<head>
<title>test</title>
<script type='text/javascript' src='js/jquery-1.4.2.js'></script>
<script>
$(window).load(function() {
jQuery('.addchar').live('hover', function(event) {
$("#result").html("Index is:" +$(this).index() );
});
});
});
</script>
</head>
<body>
<div id="content">
<div>
<ul>
<li class="addchar">one </li>
<li class="addchar">two </li>
<li class="addchar" style="display:none"> three&l开发者_如何学运维t;/li>
<li class="addchar">four </li>
<li class="addchar">five </li>
</ul>
</div>
<div id="result"></div>
</div>
</body>
</html>
How about using the visible selector:
$('.addchar:visible').live( ...
EDIT:
Too bad, you could try an alternative approach if that is an option for you:
$(function() {
$('.addchar:visible').each(function(index) {
$(this).hover(function() {
$("#result").html("Index is: " + index);
});
});
});
As foreach method iterates every element it became slow when element size rises.I searched and got it working.Thanks to this link.
jQuery('.addchar').live('hover', function (event) {
var cInd = $(this).index();//current index including hidden element
var hidLen = $(this).parent().children("li:lt(" + cInd + "):not(:visible)").length;//total hidden before hovered element
var index = cInd - hidLen;
$("#result").html("Current index :" + cInd +"<br>Tot Hidden Before Current:" + hidLen+"<br> Actual Index:"+index);
});
精彩评论