简体   繁体   English

JavaScript删除div(removeChild)

[英]Javascript delete a div (removeChild)

So first of all I'm creating some divs dynamically (an event handler for some button) in a javascript function, I'm adding a button inside each div 首先,我要在javascript函数中动态创建一些div(某个按钮的事件处理程序),然后在每个div内添加一个按钮

    newdiv.id = counter;
    newdiv.innerHTML = "Product " + (counter + 1) + " <br><input type='text' name='myInputs[]'><button onclick=\"return false;
    document.getElementById('"+counter+"').parentNode.removeChild('"+counter+"');\">Delete item</button>";

that suppose to delete that div when the button is clicked. 假设单击按钮时删除该div。

It's not working for me. 它对我不起作用。 Would appretiate some help here guys. 会在这里对大家有所帮助。

Thanks 谢谢

Your onclick event returns before doing the removeChild() call 您的onclick事件在执行removeChild()调用之前返回

If you write it in a clearer way you get: 如果您以更清晰的方式编写它,则会得到:

function () {
  // leave the function
  return false; 
  // never executed, because you called return right before
  document.getElementById(counter).parentNode.removeChild(counter);
}

You have to return after you've done whatever you want. 完成所需的操作后,您必须返回。 Move the return false; return false;移为return false; to the end of the event. 到活动结束

return ends the execution of the javascript. return结束了javascript的执行。 Nothing after the return statement is going to execute. return语句之后什么也不会执行。

By default, a button in a form is type submit. 默认情况下,表单中的按钮的类型为Submit。 To stop it submitting the form, make it type="button" . 要停止提交表单,请type="button" Then you don't need return false , which, if it was needed, should be at the end of the function. 然后,您不需要return false ,如果需要,该return false应该在函数的末尾。

An alternative is to use an input type button. 一种替代方法是使用输入类型按钮。 It looks exactly like a button and also will not submit the form. 它看起来就像一个按钮,也不会提交表单。

It would be far better to use DOM methods to add the extra elements and the listener, but here's an innerHTML version that is a bit easier to read (to me at least): 使用DOM方法添加额外的元素和侦听器会好得多,但这是一个innerHTML版本,该版本更容易阅读(至少对我而言):

newdiv.innerHTML = 'Product ' + (counter + 1) + '<br><input type="text" name="myInputs[]">' + 
                   '<button type="button" onclick="var el = document.getElementById(\'' + (counter + 1) + '\');' +
                   'el.parentNode.removeChild(el);">Delete item</button>';

Aside from the wrong order of the statements, the removeChild call is incorrect. 除了错误的语句顺序外, removeChild调用也不正确。 You have to pass a DOM element to removeChild , not an ID. 您必须将DOM元素传递给removeChild ,而不是ID。

While you could generate all the elements with an HTML string, I find creating DOM elements directly a cleaner approach when dealing with event handlers: 尽管您可以使用HTML字符串生成所有元素,但我发现在处理事件处理程序时直接创建DOM元素是一种更简洁的方法:

newdiv.id = counter;
newdiv.innerHTML = "Product " + (counter + 1) + " <br><input type='text' name='myInputs[]'>";

var button = document.createElement('button');
button.type = 'button'; // see RobG's answer
button.innerHTML = 'Delete item';
button.onclick = function() {
    this.parentNode.parentNode.removeChild(this.parentNode);
};

newdiv.appendChild(button);

Looks like you're returning false before your code gets a chance to execute (try putting it after the document.getElementById statement chain). 看起来您在代码获得执行机会之前就返回了false(尝试将其放在document.getElementById语句链之后)。 Also, I wouldn't begin any HTML attribute with a plus symbol. 另外,我不会以加号开头任何HTML属性。

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

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