Jquery,添加&悬停时删除元素
我遇到了一个问题,我尝试了几乎所有方法都没有解决。
$('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()的许多版本,但没有成功。如果有人能帮助我解决这个问题,我会很高兴。
另外,我想添加 fadeIn() 和 fadeOut() 的效果,但当然这也没有成功。
我可以添加图像,但无法删除它(即使是 fadeIn 也不起作用,而我可以成功添加图像)。
感谢您提前提供的帮助和时间。
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" />');
});
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.
Additionally I would like to add effect of fadeIn() and fadeOut() but of course this wasn't successful also.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你真的不想这样做。如果您只显示和隐藏始终存在的图像,您的代码将更快、更清晰且更易于维护。
只需将
img
放入您的 html 中并设置 id 和style="display: none;"
即可隐藏它。然后你的 JavaScript 代码就变成了:
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.
Just put the
img
into your html with and id set andstyle="display: none;"
to hide it.Your javascript code then becomes:
为您的元素命名,这会让您的生活更轻松,即:
Name you elements, it'll make life easier for you, i.e.:
你不能以这种方式引用创建的对象,尝试一下,
是的,当我写的时候,另外两个人发布了相同的东西:)
You cant reference to created object in such manner, try
Yep, and while I write, two other post the same stuff :)
.remove() 不使用 HTML 片段:
.remove() doesn't take a piece of HTML:
为什么不这样做呢?
why not just do this?