简体   繁体   English

Javascript事件:getElementByID找不到插入的元素

[英]Javascript event: getElementByID does not find inserted element

Mystery: I can't seem to pass an id to an inserted element and then remove that element using the id. 谜:我似乎无法将ID传递给插入的元素,然后使用ID删除该元素。 I get the error: 我得到错误:

"Uncaught TypeError: Cannot read property 'remove' of null" “未捕获的TypeError:无法读取null的属性'remove'

So the getElementById(thisCorrection) is not finding the element defined by addButton.id=thisCorrection . 因此, getElementById(thisCorrection)找不到由addButton.id=thisCorrection定义的元素。 Weird! 奇怪的!

What I am trying to do: click on a button, which will insert a 2nd button (b2), make an id for b2 by incrementing a counter, mouseover b2 button will remove the button. 我想做的是:单击一个按钮,该按钮将插入第二个按钮(b2),通过增加计数器来为b2设置ID,将鼠标悬停在b2按钮上将其删除。 Do it all in Javascript, not jQuery. 使用Javascript(而不是jQuery)完成所有操作。

(I thought this was simple!) (我认为这很简单!)

 correctionID=0 function removeButton(thisCorrection){ document.getElementById(thisCorrection).remove() } function checkInput() { correctionID += 1; thisCorrection="correction" + correctionID.toString(); var addButton=document.createElement("button"); addButton.id=thisCorrection; addButton.innerHTML="here is the new button"; addButton.addEventListener ("mouseenter", removeButton(thisCorrection), false); this.parentElement.appendChild(addButton) ;} 
 <div> <button onclick="checkInput()"><span style='background:yellow'>click to add button </span></button> </div> 

Here is a simpler form that shows incredible javascript/dom weirdness (for a novice). 这是一个更简单的表格,显示了令人难以置信的javascript / dom怪异度(对于新手而言)。 Looks like I am going to have to use Jquery... 看来我将不得不使用Jquery ...

 correctionID=0 function removeButton(addButton){ //addButton.remove(); // Wow! Uncomment the line above and no new button appears. // that's because clicking on button 1 runs // removeButton even though the event is // attached to the new button not to button 1! console.log(addButton.id); } function checkInput(btn1) { correctionID += 1; thisCorrection="correction" + correctionID.toString(); var addButton=document.createElement("button"); btn1.parentElement.appendChild(addButton) ; addButton.id=thisCorrection; addButton.innerHTML="Here is the new button"; addButton.addEventListener ("dblclick",removeButton(addButton), false); }; 
 <div> <button onclick="checkInput(this)"><span style='background:yellow'>click to add button </span></button> </div> 

Here is the answer... (after much blood, sweat, tears and Googling!) 答案就在这里...(经过大量的血液,汗水,眼泪和谷歌搜索!)

Why wasn't it working before? 为什么以前不起作用? The argument to the function is an event, not a button! 该函数的参数是事件,而不是按钮!

 correctionID=0 function removeButton(evt) { addButton=evt.target; //cannot says evt.remove() ! console.log(evt.target.id); addButton.remove(); } function checkInput(btn1) { correctionID += 1; thisCorrection="correction" + correctionID.toString(); var addButton=document.createElement("button"); btn1.parentElement.appendChild(addButton) ; addButton.id=thisCorrection; addButton.innerHTML="Here is the new button"; addButton.addEventListener("click",removeButton, false); }; 
 <div> <button onclick="checkInput(this)"><span style='background:yellow'>click to add button </span></button> </div> 

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

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