胡子哥哥
在html页面上实现拖放行为时,通常需要这种类型的行为。以下解决方案在MS Windows XP计算机上的IE 8.0.6,FireFox 3.6.6,Opera 10.53和Safari 4上进行了测试。首先是Peter-Paul Koch的一个小功能; 跨浏览器事件处理程序function addEvent(obj, evt, fn) {
if (obj.addEventListener) {
obj.addEventListener(evt, fn, false);
}
else if (obj.attachEvent) {
obj.attachEvent("on" + evt, fn);
}}然后使用此方法将事件处理程序附加到文档对象mouseout事件:addEvent(document, "mouseout", function(e) {
e = e ? e : window.event;
var from = e.relatedTarget || e.toElement;
if (!from || from.nodeName == "HTML") {
// stop your drag event here
// for now we can just use an alert
alert("left window");
}});最后,这是一个html页面,其中嵌入了用于调试的脚本:<html><head><script type="text/javascript">function addEvent(obj, evt, fn) {
if (obj.addEventListener) {
obj.addEventListener(evt, fn, false);
}
else if (obj.attachEvent) {
obj.attachEvent("on" + evt, fn);
}}addEvent(window,"load",function(e) {
addEvent(document, "mouseout", function(e) {
e = e ? e : window.event;
var from = e.relatedTarget || e.toElement;
if (!from || from.nodeName == "HTML") {
// stop your drag event here
// for now we can just use an alert
alert("left window");
}
});});</script></head><body></body></html>