jQuery 事件处理程序未在 IE 中触发
我在页面上有一个项目列表,其中包含一组上移、下移和删除控件。
这些控件位于默认情况下隐藏的列表顶部。 当您将鼠标悬停在项目行上时,我会使用 jquery 选择控件,
//doc ready function:
..
var tools = $('#tools');
$('#moveup').click(MoveUp);
$('#movedn').click(MoveDn);
$('#delete').click(Delete);
..
$('li.item').mouseover(function(){
$(this).prepend(tools);
});
这在 Firefox 中效果很好。工具移动到当前行,单击事件调用 ajax 函数。 然而,在IE6和IE7中..没有点击发生。 我尝试在鼠标移出时解除绑定并在每次鼠标悬停时重新绑定..但无济于事。
我还研究了 javascript 之外的各种原因(例如透明 png 冲突、z-index、位置:绝对)..也没有找到解决方案。
我最终需要向每个项目添加一个工具行,并在鼠标悬停/移出时显示/隐藏。 效果也很好——唯一令人沮丧的是我的页面上有更多的“工具”标记。
有谁知道为什么一旦对象移动(使用前置)IE 就会忽略/删除/终止鼠标事件? 为什么事后重新绑定事件也没有效果? 让我恼火了将近2个小时才放弃。
I have a list of items on a page with a set of controls to MoveUp, MoveDown and Delete.
The controls sit at the top of list hidden by default. As you mouseover an item row, I select the controls with jquery
//doc ready function:
..
var tools = $('#tools');
$('#moveup').click(MoveUp);
$('#movedn').click(MoveDn);
$('#delete').click(Delete);
..
$('li.item').mouseover(function(){
$(this).prepend(tools);
});
This works great in Firefox .. the tools move into the current row, and the click events call the ajax functions. However, in IE6 and IE7 .. no click occurs. I tried unbinding on mouseout and rebinding on each mouseover .. to no avail.
I also looked into various reasons outside of javascript (e.g. transparent png conflicts, z-index, position:absolute) .. also no solution found.
I eventually needed to add a tools row to each item and show/hide on mouse over/out. Works just as well -- the only downer is that I have much more 'tools' markup on my page.
Does anyone know why IE ignores/drops/kills the mouse events once the objects are moved (using prepend)? And why rebinding the event afterwards also has no effect? Kept me annoyed for almost 2 hours before I gave up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IE 将丢失事件,具体取决于您向 DOM 添加内容的方式。
我还建议在点击事件上使用 .live() 来稍微简化您的代码。 live 尚不支持鼠标悬停/移出。 http://docs.jquery.com/Events/live
IE will lose events depending on how you are adding things to the DOM.
I would also recommend using .live() on the click events to simplify your code a little. Mouseover/out is not supported by live yet. http://docs.jquery.com/Events/live
我只是花了一整天的时间对未触发附加到 DOM 的项目(IE7、jQuery 1.4.1)的事件进行故障排除,这并不是因为我需要使用 live()(不过,很高兴知道,Chad),也不是因为因为我需要克隆这些项目。
这是因为我选择的锚标记中包含“#”,如下所示:
我的解决方案是使用“属性包含选择器”,如下所示:
幸运的是,我对所有内容都有足够的控制权,将来不太可能破坏。 这在技术上与附加对象无关,但希望它可以节省一些时间。
I just spent the whole day troubleshooting events not triggering on items appended to the DOM, (IE7, jQuery 1.4.1) and it wasn't because I needed to use live() (though, good to know, Chad), nor was it because I needed to clone the items.
It was because I was selecting anchor tags that had a "#" in them like so:
My solution was to use the "Attribute Contains Selector" like so:
Fortunately I have enough control over everything that it won't likely break in the future. This isn't technically related to appended objects, but hopefully it saves someone some time.
我有类似的问题。 尝试使用 .ready 在初始页面加载时加载 div。
在 FF 中运行良好,但在 ie7 中运行不佳。
我发现了一个似乎可以解决这个问题的技巧。
我有加载调用回调,divLoaded()。
在 divLoaded 中,我检查 $('#targetdiv').innerText.length < 50 或任何你认为表明它没有加载的值。 如果我检测到这种情况,我只需调用再次加载该 div 的函数即可。
奇怪的是,我还添加了一个“.” 在我回想起 ajax 函数之前,先到innerText。 有时我们似乎会经历 3 或 4 个循环才能最终完成 ajax 加载。
这让我认为 document.ready 在 IE7 中工作得非常完美,这似乎消除了一些关于它不可靠的神话。 真正“似乎”发生的是 .load 有点不稳定,并且在页面刚刚加载时无法正常工作。
我对 jQuery 的东西还是有点陌生,所以对此持保留态度。 有兴趣听听任何人对我的小假设的看法。
干杯
格雷格
i had a similar problem. trying to use .ready to load a div on the initial page load.
works well in FF , but not ie7.
i have found a hack that seems to get around this.
I have load call a callback, divLoaded().
In divLoaded i check the $('#targetdiv').innerText.length < 50 or whatever you think will indicate that it didnt load. If I detect that case, i simply call the function taht loads that div again.
Oddly enough, i also add a '.' to the innerText before i recall the ajax function. It seems taht sometimes we go through 3 or 4 loops before the ajax load finally takes.
This leads me to think that document.ready works pretty flawlessly in IE7, which seems to dispel a bit of a myth that it is unreliable. What really 'seems' to be happening is that .load is a little bit flakey and doesnt work well when the page is just loaded.
I am still a bit green w/ all the jQuery stuff, so take this w/ a grain of salt. Interested to hear anyone's take on my little hypothesis.
cheers
greg