【发布时间】:2013-05-21 00:42:46
【问题描述】:
我的页面上有这样的 HTML 代码:
<li><img width="100" alt="3Caballeros.png" onmouseover="show_buttons();"</img></li>
当用户悬停图像时,基本上应该会显示几个按钮。 *show_buttons()* 方法应该设置它。这里是:
function show_buttons(item){
var parent =$('img[alt="'+item.alt+'"]').parent();
alert(parent.prop('tagName')); //says LI
parent.find('img').fadeIn(); //shows the other images in the LI (they are added by JavaScript)
item.onmouseover =function () {}; //removes the event listener to the image
parent.mouseout( parent, function(){
parent.find('img[alt!="'+item.alt+'"]').fadeOut();
});
parent.mouseover( parent, function(){
parent.find('img[alt!="'+item.alt+'"]').fadeIn();
});
}
使用 prepend() 方法添加图像,结果类似于:
<li><img class="first_button" src="images/first.png" style="display: none;">
<img class="second_button" src="images/second.png" style="display: none;">
<img width="100" alt="3Caballeros.png" onmouseover="show_buttons();"</img></li>
所以代码几乎没问题:图像在光标进入 LI 时显示,离开时消失。问题是,当光标悬停在 LI 中动态添加的图像之一时,会触发 mouseover 事件,因此图像会消失。这些图像在 LI 中,为什么会触发事件?
我该如何解决这个问题? 谢谢
【问题讨论】:
-
你能再显示一些代码吗?例如。这些图像究竟是如何动态添加的...
-
我刚刚编辑了帖子。
-
当我将fadeIn() 和fadeOut() 替换为show() 和hide() 时,它可以工作......但我仍然希望我的问题得到解答。
-
嗯...我刚看到这个评论。就像我在回答中所说的那样,除非我对设置(元素布局、CSS 属性等)有更多了解,否则我会在黑暗中拍摄 :)
标签: jquery mouseevent mouseout