我正在尝试将处理程序应用于 iframe 中的所有元素(位于同一域中),但我无法弄清楚为什么该函数仅在单击时触发。我担心这可能与 iframe 仅在我单击时才处于事件状态有关。我已经在 jsfiddle 中看到了它的应用程序,例如 http://jsfiddle.net/danmana/pMBw2/
这基本上是我的代码(我尝试使用委托(delegate)函数模仿 jsfiddle 并得到相同的结果):
$('iframe').contents().on("mouseover", "*", function() {
$(this).css("background-color", "yellow");
});
编辑 使用那个 jsfiddle ( http://jsfiddle.net/danmana/pMBw2/ ) 我发现如果你将它切换到最新版本的 jquery 2.1.0,那么代码就不再有效,似乎 1.8.3 是使用该代码的最新版本。
最佳答案
模仿你提到的 jsfiddle 应该工作得很好,你必须使用 delegate()
函数:
<iframe src="http://fiddle.jshell.net"></iframe>
var $c = $('iframe').contents();
$c.delegate('div', "hover", function() {
$(this).css("background-color", "yellow");
});
关于javascript - 鼠标悬停在框架中的所有元素上仅响应点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29090724/