jQuery,添加&悬停时删除元素 [英] Jquery, add & remove element on hover

查看:98
本文介绍了jQuery,添加&悬停时删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我几乎解决了所有问题,没有解决办法.

I came a cross a problem which I tried pretty much everything without a solution.

$('a.hovered').hover(function () {
    $(this).after(' <img src="images/icons/famfamfam/silk/user_go.png" />');
},function () {
    $(this).remove(' <img src="images/icons/famfamfam/silk/user_go.png" />');
});

当然,我尝试了这个remove()的许多版本,但都没有成功.如果有人可以帮助我解决这个问题,我将感到很高兴.

Of course I tried many versions of this remove() without any success. I will be glad if anyone could help me out with this problem.

另外,我想添加fadeIn()和fadeOut()的效果,但是当然这也不成功.

Additionally I would like to add effect of fadeIn() and fadeOut() but of course this wasn't successful also.

我可以添加图像,但不能删除它(即使我可以成功添加图像,即使fadeIn也无法正常工作).

I can add the image but I can't remove it (even fadeIn didn't work while I can successfully add images).

感谢您的帮助和提前的时间.

Thank you for your help and time in advance.

推荐答案

您真的不想这样做.如果仅显示和隐藏始终存在的图像,您的代码将更加快捷,清晰和易于维护.

You really don't want to do that. Your code will be quicker, clearer and easier to maintain if you just show and hide an image that always exist.

只需将img放入html中,并设置ID和id,然后将style="display: none;"隐藏即可.

Just put the img into your html with and id set and style="display: none;" to hide it.

您的JavaScript代码随即变为:

Your javascript code then becomes:

$('a.hovered').hover(function () {
    $("#user_go").show();
},function () {
    $("#user_go").hide();
});

这篇关于jQuery,添加&amp;悬停时删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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