Javascript 使用按钮事件处理程序删除行

Javascript 使用按钮事件处理程序删除行,javascript,jquery,html,Javascript,Jquery,Html,好的,我需要删除按钮的行/div onclick。 到目前为止,我有: <h2>Header 3</h2> <div id="results3" class="results"> <h1>Results 3</h1> <div id="1">Item 1 <button type="button" onclick="remove()">Remove</button&

好的,我需要删除按钮的
行/div onclick
。 到目前为止,我有:

<h2>Header 3</h2>

    <div id="results3" class="results">
        <h1>Results 3</h1>
        <div id="1">Item 1 <button type="button" onclick="remove()">Remove</button></div>
        <div id="2">Item 2 <button type="button" onclick="remove()">Remove</button></div>
        <div id="3">Item 3 <button type="button" onclick="remove()">Remove</button></div>
        <div id="4">Item 4 <button type="button" onclick="remove()">Remove</button></div>
    </div>

因为您标记了jQuery,所以我认为jQuery解决方案适合您:

在按钮中添加一个
class=“remove”
,并使用此javascript

$(function() {
    $('button.remove').click(function() {
         $(this).closest('div').remove();
    });
});
演示:

HTML

html

<div id="1">Item 1 
    <button type="button" onclick="remove(this)">Remove</button>
</div>

你的脚本在哪里?我还没有任何功能脚本,我现在已经在上面添加了它。帮你自己一个忙,不要使用内联事件绑定(
onclick=“…
)并在脚本中使用集中事件绑定。感谢大家的帮助。jQuery解决方案非常适合这种情况,但是所有答案都很好!或
$(“.results div button”)。单击()
将与现有结构一起工作确实如此,但我更喜欢在选择器中更加具体,因为它不太容易出错(在进一步开发页面时)。
<button type="button" onclick="remove(this)">Remove</button>
var remove = function(btn){
    btn.parentNode.style.display="none";
    //Remove the div
    //var div =  btn.parentNode;
    //div.parentNode.removeChild(div);
}  
<div id="1">Item 1 
    <button type="button" onclick="remove(this)">Remove</button>
</div>
 function remove(removeMe)
  {
      $(removeMe).remove();
  }