我有一个 div 正在等待移动事件。然后放置一个带有信息的 div。
我遇到的问题是要正确删除事件侦听器并删除它创建的 div ...由于某种原因它找不到我制作的子 div。
所以这是我尝试过的脚本:
div.addEventListener('mouseover',bubble_info,false);
function bubble_info(e){
var x = e.pageX + 50; //push it 50px to the right
var div = document.createElement('div');
div.style.position = 'absolute';
div.style.top = e.pageY;
div.style.left = x;
div.className = 'bubble';
div.innerHTML = 'Testing this div';
this.appendChild(div);
//stop firing if mouse moves around on the parent as it is still "over" the parent
this.removeEventListener('mouseover',bubble_info,false);
//when mouse out occurs the below should fire
this.addEventListener('mouseout',function(){clear_hover.call(this,div);},false);
}
function clear_hover(child){
//remove the bubble div we made
child.parentNode.removeChild(child);
//remove the mouse out as we are now out
this.removeEventListener('mouseout',function(){clear_hover.call(this,div);},false);
//re-add the mouseover listener encase user hovers over it again
this.addEventListener('mouseover',bubble_info,false);
}
任何人都可以看到我在这里犯的错误,只是无法弄清楚为什么鼠标退出会出错。
开发工具展示Cannot call method 'removeChild' of null