日期:2014-05-16  浏览次数:20405 次

关于如何只触发当前元素?
本帖最后由 mixi123456 于 2013-06-13 12:41:51 编辑
想要实现的效果是鼠标悬浮在当前元素时,只有当前元素触发。
这段代码的问题是只有当第一次悬浮时是只触发了当前元素,而再次悬浮时,就连之前触发过的元素也触发了,请问各位这是为什么呢?


function hider(obj) {
jQuery("div.comment-item").mouseover(function() {
$(obj).find(".op-lnks").css("visibility","visible");
}); 
jQuery("div.comment-item").mouseout(function() {
$(obj).find(".op-lnks").css("visibility","hidden");
}); 
}


<div class="comment-item" onmouseover="hider(this);">
<div>user1</div>
    <div style="visibility:hidden" class="op-lnks">
        <a href="">回应</a>
    </div> 
</div>
</div>
<div class="comment-item" onmouseover="hider(this);">
<div>user2</div>
    <div style="visibility:hidden" class="op-lnks">
        <a href="">回应</a>
    </div> 
</div>
</div>
<div class="comment-item" onmouseover="hider(this);">
<div>user3</div>
    <div style="visibility:hidden" class="op-lnks">
        <a href="">回应</a>
    </div> 
</div>
</div>


------解决方案--------------------

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript">
$(function(){
jQuery("div.comment-item").mouseover(function() {
$(this).find(".op-lnks").css("visibility","visible");
}).mouseout(function() {
$(this).find(".op-lnks").css("visibility","hidden");
}); 
});
</script>

<div class="comment-item">
<div>user1</div>
    <div style="visibility: hidden" class="op-lnks">
        <a href="">回应</a>
    </div> 
</div>
</div>
<div class="comment-item">
<div>user2</div>
    <div style="visibility: hidden" class="op-lnks">
        <a href="">回应</a>
    </div> 
</div>
</div>
<div class="comment-item">
<div>user3</div>
    <div style="visibility: hidden" class="op-lnks">
        <a href="">回应</a>
    </div> 
</div>
</div>