简体   繁体   English

Javascript:除非元素附加到document.body,否则不会触发事件

[英]Javascript: Event does not fire unless element appended to document.body

I dynamically create an element (div) in javascript, on which i register an event listener: 我在javascript中动态创建一个元素(div),在该元素上注册了一个事件侦听器:

var tooltip = document.createElement('div');
tooltip.onclick = function() { alert('hello'); } 

Now, if I attach this element to the document body: 现在,如果我将此元素附加到文档主体:

document.body.appendChild(tooltip);

all is well and the event is captured. 一切都很好,并捕获了事件。 However (for positioning purposes) i want to attach this element to a (static) sub-element within my page, eg: 但是(出于定位目的)我想将此元素附加到页面中的(静态)子元素上,例如:

document.getElementById('id').appendChild(tooltip);

and the element is generated and positioned correctly - but the onclick event now is no longer captured. 并且该元素已正确生成和定位-但现在不再捕获onclick事件。 Any thoughts? 有什么想法吗? This is x-browser, so i must be missing something. 这是X浏览器,所以我必须缺少一些东西。

Thanks, Don. 谢谢,唐。

也许您需要在追加后注册事件处理程序?

You're creating not only one but MANY divs. 您不仅要创建一个div,而且要创建许多div。 Try this instead(I hope you don't mind but I fixed the HTML and CSS too): 尝试以下方法(我希望您不介意,但我也修复了HTML和CSS):

<html>
<head>

<script type="text/javascript">

function makeDiv() {
    if(!document.getElementById('tooltipDiv')){
        var tooltip = document.createElement('div');

        tooltip.id = "tooltipDiv";
        // Give our tooltip a size and colour so we can see it
        tooltip.style.height = '200px';
        tooltip.style.position = 'absolute';
        tooltip.style.width = '200px';
        tooltip.style.backgroundColor = '#eee';

        // Register onclick listener
        tooltip.onclick = function() { alert('hello'); }
        //tooltip.addEventListener("click", function(){ alert('hello'); }, false);

        // *** Comment one of these out: ***

        //document.body.appendChild(tooltip);
        document.getElementById('myDiv').appendChild(tooltip);
    }
}

</script>
</head>

<body>


<div id="myDiv" 
     onmouseover="makeDiv();" 
     style="position: relative; top: 100px; left: 100px; border: 1px solid red; width: 200px;">

<span>my div text</span>

</div>

</body>
</html>

Your code works fine for me on firefox 3.0.5 and IE7. 您的代码对我来说在firefox 3.0.5和IE7上工作正常。 Are you sure your example is correct? 您确定您的示例正确吗?

Ok all, here is my code, apologies for the delay. 好的,这是我的代码,对于延迟,我们深表歉意。 A version with a work-around is posted underneath: 具有解决方法的版本发布在下面:

<html>
<head>

<script type="text/javascript">

function makeDiv() {
  var tooltip = document.createElement('div');

  // Give our tooltip a size and colour so we can see it
  tooltip.style.height = '200px';
  tooltip.style.position = 'absolute';
  tooltip.style.width = '200px';
  tooltip.style.backgroundColor = '#eee';

  // Register onclick listener
  tooltip.onclick = function() { alert('hello'); }

  // *** Comment one of these out: ***

  //document.body.appendChild(tooltip);
  document.getElementById('myDiv').appendChild(tooltip);
}

</script>
</head>

<body>


<div id="myDiv" 
     onmouseover="makeDiv();" 
     style="position: relative; top: 100px; left; 100px; border: 1px solid red; width: 200px;">

<span>my div text</span>

</div>

</body>
</html>

=================================== OK - so this works: ==================================可以-这样就可以了:

<html>
<head>

<script type="text/javascript">

function makeDiv() {
  var tooltip = document.createElement('div');

  // Give our tooltip a size and colour so we can see it
  tooltip.style.height = '200px';
  tooltip.style.position = 'absolute';
  tooltip.style.width = '200px';
  tooltip.style.backgroundColor = '#eee';

  // Register onclick listener
  tooltip.onclick = function() { alert('hello'); }

  // *** Comment one of these out: ***

  //document.body.appendChild(tooltip);
  document.getElementById('container').appendChild(tooltip);
}

</script>
</head>

<body>

<div id="container" style="border: 1px solid blue; float: left; ">
  <div id="myDiv" 
       onmouseover="makeDiv();" 
       style="position: relative; border: 1px solid red; width: 200px;">

       <span>my div text</span>
  </div>
</div>

</body>
</html>

Here is some code to remove the tooltip for onmouseout. 这是一些代码,用于删除onmouseout的工具提示。

Give your toolTip an ID when creating it: 创建工具提示时给它一个ID:

toolTip.setAttribute('id','toolTip');

Then for onmouseout 然后进行鼠标暂停

function removeDiv(container) {
    var toolTip = document.getElementById('toolTip');
    document.getElementById(container).removeChild(toolTip);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 $(document.body)在javascript中的含义是什么? - What does $(document.body) mean in javascript? 将大量元素事件侦听器委托给 document.body 是不是很疯狂? - Is it crazy to delegate tons of element event listeners to document.body? jQuery选择器$(“element”,$(document.body))有什么作用? - What does the jQuery selector $(“element”, $(document.body)) do? 单击附加到 document.body 的事件侦听器不会在 iOS6 中触发? - click event listeners attached to document.body dont fire in iOS6? 奇怪的document.body上的onload事件 - onload event on document.body behaving strange 为什么我的javascript中的document.body为null? - Why is document.body null in my javascript? 为什么我需要使用Mootools Element方法扩展document.body $(document.body)? - Why do I need to do $(document.body) to extend document.body with Mootools Element methods? 点击事件并不总是传播到移动设备上的document.body - Click-event does not always propagate to document.body on mobile devices 如何找到使用 Javascript 创建但不存在于 document.body 中的元素? - How can I locate an element which was created with Javascript and is not present in the document.body? 为什么document.body的事件处理程序中的'this'关键字引用全局window object? - Why does the 'this' keyword inside event handlers of document.body refer to the global window object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM