使用jQuery在鼠标悬停时显示隐藏类
问题描述:
我是jQuery的新手,我希望能够在鼠标悬停时显示一个菜单。
I am relatively new to jQuery, and I would like to be able to show a menu on mouseover.
以下是HTML内容:
<td class ="comment_div"> <?php echo("$comment_data['comment']); ?> <br/>
<span class="comment_actions"> Approve | Delete | Spam | Edit</span>
</td>
然后使用jQuery代码:
Then the jQuery code:
$("comment_div").hover(
function() { $(".comment_actions").show(); },
function() { $(".comment_actions").hide(); }
);
这是可行的,除了我要拉出多个注释,并且无论悬停了什么注释,它只会在第一个div上显示菜单。我希望菜单仅针对当前注释显示我想我需要使用 $ this来完成这项工作,但我不确定如何使用。
This works except for I'm pulling multiple comments out and this only will show the menu on the first div no matter what "comment" is hovered. I would like to have the menu show only for the comment that is currently being hovered over. I think I need to use "$this" to make this work, but I am not sure how.
答
如果我没看错,格式应为-
If I'm reading that correctly, the format should be-
$(".comment_div").hover(
function() { $(this).children(".comment_actions").show(); },
function() { $(this).children(".comment_actions").hide(); }
);