jquery mouseleave 导致其他脚本停止工作?
我正在编写一个评级脚本,以便可以对项目进行 1 到 5 星评级。我的脚本首先起作用,导致星星从当前评级更改为鼠标悬停的任何评级,然后在鼠标移出时返回当前评级。但随后,盘旋回到星星上空并不会造成任何变化。 (同时没有点击。)
因此,悬停有效,然后 mouseleave 有效,然后悬停不再有效,尽管 mouseleave 继续工作。这是我的 javascript 文件中的代码:
$(document).ready(function(){
$("img.rateImage").hover(function(){
$(this).attr("src","images/vote-star.png");
$(this).prevAll("img.rateImage").attr("src","images/vote-star.png");
$(this).nextAll("img.rateImage").attr("src","images/vote-star-off.png");
});
$("div#invRate").mouseleave(function(){
var rate = $(this).attr("ref");
var i = 1;
var imgs = "";
while (rate > 0){
imgs += "<img class=\"rateImage\" id=\"vote"+i+"\" src=\"images/vote-star.png\" alt=\""+i+"\" style=\"cursor:pointer;\" /> ";
i++;
rate--;
}
while (i < 6){
imgs += "<img class=\"rateImage\" id=\"vote"+i+"\" src=\"images/vote-star-off.png\" alt=\""+i+"\" style=\"cursor:pointer;\" /> ";
i++;
}
$(this).html(imgs);
});
});
这是 div 标签和代码,所有这些都发生在其中:
<div style="display:block; text-align:center;" id="invRate" ref="<?= $curRate; ?>">
<?
while ($curRate > 0){?>
<img class="rateImage" id="vote<?= $i ?>" src="images/vote-star.png" alt="<?= $i ?>" style="cursor:pointer;" />
<? $i ++;
$curRate --;
}
while ($i < 6){?>
<img class="rateImage" id="vote<?= $i ?>" src="images/vote-star-off.png" alt="<?= $i ?>" style="cursor:pointer;" />
<? $i ++;
}
?>
</div>
是否有任何原因 mouseleave 事件会导致悬停事件停止工作? mouseleave 事件传回 div 标签的 html 与开始时的 html 完全相同。所以我有点迷失了。
预先感谢您的任何建议和帮助。
编辑
根据安东尼的建议,我研究了 live() 但发现它已被弃用,我应该使用 on()。委托方法还建议使用 on()。所以我就这么选择了。在遇到几个错误后,我意识到我的 jquery.js 太旧了,所以我更新了它,它就像一个魅力。我只将ready(function(){})内部的第一行更改为:
$("#invRate").on("hover","img.rateImage",function(){
I am writing a rater script so items can be rated 1 through 5 stars. My script works at first, causing the stars to change from the current rating to whatever they are moused over, and then back to the current rating when they mouse out. But then, hovering back over the stars causes no change. (No clicks in the meantime.)
So hover works, then mouseleave works, then hover no longer works although mouseleave continues to work. Here is my code from the javascript file:
$(document).ready(function(){
$("img.rateImage").hover(function(){
$(this).attr("src","images/vote-star.png");
$(this).prevAll("img.rateImage").attr("src","images/vote-star.png");
$(this).nextAll("img.rateImage").attr("src","images/vote-star-off.png");
});
$("div#invRate").mouseleave(function(){
var rate = $(this).attr("ref");
var i = 1;
var imgs = "";
while (rate > 0){
imgs += "<img class=\"rateImage\" id=\"vote"+i+"\" src=\"images/vote-star.png\" alt=\""+i+"\" style=\"cursor:pointer;\" /> ";
i++;
rate--;
}
while (i < 6){
imgs += "<img class=\"rateImage\" id=\"vote"+i+"\" src=\"images/vote-star-off.png\" alt=\""+i+"\" style=\"cursor:pointer;\" /> ";
i++;
}
$(this).html(imgs);
});
});
And here is the div tag and code where all of this is happening:
<div style="display:block; text-align:center;" id="invRate" ref="<?= $curRate; ?>">
<?
while ($curRate > 0){?>
<img class="rateImage" id="vote<?= $i ?>" src="images/vote-star.png" alt="<?= $i ?>" style="cursor:pointer;" />
<? $i ++;
$curRate --;
}
while ($i < 6){?>
<img class="rateImage" id="vote<?= $i ?>" src="images/vote-star-off.png" alt="<?= $i ?>" style="cursor:pointer;" />
<? $i ++;
}
?>
</div>
Is there any reason the mouseleave event would cause the hover event to stop working? The html that the mouseleave event passes back to the div tag is the exact same html as what is there to begin with. So I'm kind of lost.
Thanks in advance for any suggestions and help.
EDIT
With Anthony's suggestions, I looked into live() but found that it was deprecated, and that I should use on(). The delegate method also suggested to use on(). So that's what I went with. After getting a couple errors, I realized that my jquery.js was just old, so I updated that and it worked like a charm. I only changed the first line inside of the ready(function(){}) to look like:
$("#invRate").on("hover","img.rateImage",function(){
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
mouseleave
事件处理程序函数正在替换 div 的 HTML,从而创建不再附加hover
事件处理程序的新 DOM 元素。您可以使用 jQuery 的live()
方法自动将事件处理程序添加到与选择器匹配的任何新创建的 DOM 元素中。Your
mouseleave
event handler function is replacing the HTML of the div, thereby creating new DOM elements that no longer have thehover
event handler attached to them. You can use jQuery'slive()
method to automatically add the event handler to any newly created DOM elements that match the selector.