Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何解决-DIV元素在渲染后立即闪烁/消失_Javascript_Javascript Events - Fatal编程技术网

Javascript 如何解决-DIV元素在渲染后立即闪烁/消失

Javascript 如何解决-DIV元素在渲染后立即闪烁/消失,javascript,javascript-events,Javascript,Javascript Events,下面是正在发生的事情 页面加载后,javascript从底层代码中读取XML XML包含一组字段id,以及当鼠标悬停在列出的字段id上时要在弹出窗口中显示的相应内容 我的代码生成一组弹出窗口,作为带有样式的div元素 .render{ background-color: #fffc80; border: .1em solid rgb(200, 128, 0); padding-left: 2px; padding-right:

下面是正在发生的事情

  • 页面加载后,javascript从底层代码中读取XML
  • XML包含一组字段id,以及当鼠标悬停在列出的字段id上时要在弹出窗口中显示的相应内容
我的代码生成一组弹出窗口,作为带有样式的div元素

.render{
 background-color:    #fffc80;
 border:             .1em solid rgb(200, 128, 0);
 padding-left:        2px;
 padding-right:       2px;
 z-index:             1000;
}

.hide{
 display:none;
}
所有创建的弹出窗口都附加到根元素

已编辑:添加脚本片段

事件处理程序如下所示

// instantiate a div element

var myDiv = document.createElement('div');

// generate an ID 

myDiv.id = generatePopupId(getFieldId());

// attach the content from the XML into the new div element

myDiv.innerHTML = getPopupContent();

// apply mouseover/out handlers to the main element

document.getElementById(getFieldId()).onmouseover = function(){
  showPopup(generatePopupId(getFieldId()));
};

document.getElementById(getFieldId()).onmouseout = function(){
  hidePopup(generatePopupId(getFieldId()));
}; 


// read the X coordinate of the present position of the mouse

function getX(){
  var e = window.event;
  posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  return posX;
}

// read the Y coordinate of the present position of the mouse

function getY(){
  var e = window.event;
  posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  return posY;
}

// Show the popup element at the current mouse location

function showPopup(popupId){
  var posX = getX();
  var posY = getY();

  var poppyElement = document.getElementById(popupId);

  poppyElement.className = 'render';
  poppyElement.style.left = posX;
  poppyElement.style.top  = poxY;
  poppyElement.style.position = 'absolute';
  poppyElement.style.display = '';

}

// hide the popup element

function hidePopup(popupId){
  var poppyElement = document.getElementById(popupId);

  poppyElement.className = 'hide';

}
我的问题是-为什么元素会闪烁,并立即消失,而不是挂在鼠标退出事件

问候,,
Abhishek更改JavaScript中的元素可能会修改悬停在上面的元素,这可能会通过更改触发鼠标移出事件,而不是实际将鼠标移出坐标。

首先,您需要更加小心区分大小写。它应该是
clientWidth
(大写W)和
top
(小t)。其次,当您设置CSS
left
top
时,必须在值中添加
+'px'
后缀;整数本身无效


另外,如果您想知道视口的高度,
document.body
是错误的查看位置。这将只在IE怪癖模式下工作,你通常希望避免像瘟疫一样的怪癖。添加标准模式
提供更多代码。你是如何连接事件处理程序的?更新了帖子,以携带我的脚本。有人吗?