简体   繁体   English

如何使用javascript中的鼠标悬停事件删除我在javascript中创建的DOM节点?

[英]How do I remove DOM nodes that I created in javascript using mouseover events in javascript?

So the HTML displays an area with a hundred little boxes places in it at random positions.所以 HTML 显示了一个区域,其中有 100 个随机位置的小框。 I've used javascript to make the little boxes.我使用 javascript 来制作小盒子。 There's also a button in the HTML that I've programmed in js to make a hundred more little boxes every time it's clicked. HTML 中还有一个按钮,我在 js 中编写了该按钮,每次单击时都会生成一百多个小框。 My issue is that I want to be able to make each little box disappear every time I hover over it, preferably using the "this" command to do so.我的问题是我希望每次将鼠标悬停在每个小框上时都能让它消失,最好使用“this”命令来做到这一点。 Am I doing anything wrong, or is javascript unable to interact with new HTML elements after it creates them?我做错了什么,还是javascript在创建新的HTML元素后无法与它们交互?

Here's my code:这是我的代码:

 window.onload = function(){
 createBoxes();
 $("myButton").observe("click", createBoxes);
 var divvy = $("container");
 var pars = divvy.getElementsByTagName("p")
 pars.onmouseover = destroyBoxes;

}
function destroyBoxes (event){
var divvy = $("container");
var pars = divvy.getElementsByTagName("p")
if (event.type == "mouseover")   {
pars.removeChild(This);
}

}



function createBoxes()
{
var colors = ["red", "green", "grey", "blue", "orange", "yellow"];
for (i=0;i<=99;i++){
var newP = document.createElement("p");
var top = Math.floor( Math.random() *400 ) + "px";
var left = Math.floor( Math.random() *400 ) + "px";
newP.style.top  = top;
newP.style.left  = left;
newP.style.backgroundColor = colors[ Math.floor( Math.random() *6 )];
$("container").appendChild(newP);
      var divvy = $("container");
      var pars = divvy.getElementsByTagName("p")
      pars.onmouseover = destroyBoxes;
    }
    }

"Try using event.target instead of This in the destroyBoxes() function." “尝试在destroyBoxes()函数中使用event.target而不是This 。”

That being said...Great job on discovering a solution on your own!话虽这么说......在自己发现解决方案方面做得很好!

If interested, here's my solution using jQuery functions.如果有兴趣,这是我使用 jQuery 函数的解决方案。

 window.onload = function() { createBoxes(); $(".myButton").on("click", createBoxes); // Use class selector //Find all `p`s in `container` //$pars.on('hover', destroyBox); } function destroyBox(event) { var $target = $(event.target); // The 'if' statement is redundant. Only run the function if wanted. //if (event.type == "mouseover") { $target.remove(); //} } function createBoxes() { var colors = ["red", "green", "grey", "blue", "orange", "yellow"]; var newPs = []; for (i = 0; i < 100; i++) { var top = Math.floor(Math.random() * 400), left = Math.floor(Math.random() * 400), color = colors[Math.floor(Math.random() * 6)]; var $newP = $('<p></p>').css({top: top, left: left, backgroundColor: color}); newPs.push($newP); } var $container = $(".container").append(newPs); // Use class selector $container.find('p').on('mouseover', destroyBox); }
 .container { position: relative; } p { position: absolute; width: 10px; height: 10px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div> <button type="button" class="myButton">Click for more!</button> </div> <div class="container"></div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM