如何使用JavaScript从页面中删除div

时间:2016-08-26 16:23:58

标签: javascript html css

½我正在尝试为动态创建的div创建一个关闭按钮。

目前下面的代码可以创建一个div重复,但我似乎无法让div关闭按钮工作。我试图这样做即使多个div打开,关闭按钮也适用于每个。

如果有办法通过jQuery,请告诉我,因为我无法让它工作。

<html>
    <title>Test Platform</title>

    <head>
        <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    </head>

    <style>
        body {
          margin: 0 0;
        }
        #container {
          border: 1px solid blue;
          white-space: nowrap;
          overflow: auto;
            font-size: 0;
        }

        #container > * {
          font-size: 8px;
          font-family: 'Open Sans', sans-serif;
        }

        #headerbar {
          font-size: 30px;
          color: white;
          padding-left: 10px;
          border: 1px solid darkgrey;
          height: 50px;
          background-color: darkslateblue;
        }
        #sidebar {
          display: inline-block;
          color: black;
          border: 1px solid darkgrey;
          width: 50px;
          height: 100vh;
          vertical-align: top;
          background-color: lightgrey;
          text-align: center;
          padding-top: 10px;
        }
        .content {
          display: inline-block;
          width: 200px;
          height: 100vh;
          border: 1px solid lightslategrey;
          margin: 0 1px 0 0;
          vertical-align: top;
          background-color: lightsteelblue;
        }
        .close {
            display: inline-block;
            padding: 2px 5px;
            background: #ededed;
            float: right;
        }
        .close:hover {
            float: right;
            display: inline-block;
            padding: 2px 5px;
            background: #ccc;
            color: #fff;
        }
    </style>

    <body>
        <div id='container'>
            <div id='headerbar'>Header Div</div>
            <div id='sidebar'> <input type="button" value="O" id="calculate" />
                <br/><br/>
                <br/><br/>
            </div>
        </div>

        <script type='text/javascript'>
            window.onload = function(){
                document.getElementById('close').onclick = function(){
                    this.parentNode.parentNode.parentNode
                    .removeChild(this.parentNode.parentNode);
                    return false;
                };
            };
        </script>

        <script>
            function makeResponseBox() {
                document.getElementById("calculate").onclick = function()
                {
                    var responseBox = document.createElement("DIV"); //create <div>
                    var spanclose = document.createElement("span");
                    var spantext = document.createTextNode("X");
                    spanclose.appendChild(spantext);
                    spanclose.setAttribute("class", "close" );
                    responseBox.setAttribute("class", "content" );
                    responseBox.appendChild(spanclose);
                    document.getElementById('container').appendChild(responseBox);
                }
            } // Close function (makeResponseBox)
            window.onload = makeResponseBox;
        </script>
    </body>
</html>

8 个答案:

答案 0 :(得分:1)

$("body").on("click", ".content .close", function() {
    $(this).parent().remove();
});

你删除动态创建的div的代码几乎可以工作,但是它正在寻找id而不是class而不会工作,因为在将内容添加到DOM之前会添加事件。

要解决这个问题,我们可以在jQuery中使用事件委托,如上所示。我们将点击附加到正文而不是关闭按钮,只有在单击关闭按钮时才会触发。

答案 1 :(得分:1)

这将是您使用jQuery的功能。请注意,您仍然需要将返回值添加到DOM。

function makeResponseBox() {
    $close = $('<div>').addClass('close');
    $window = $('<div>').addClass('window');
    $window.append($close);
    $close.on('click', function(e){
        e.preventDefault();
        // removes the enclosing div.window
        $(e.target).closest('.window').remove();
        return false;
    });
    return $window;
}

https://jsfiddle.net/p96Lq65s/17/

答案 2 :(得分:0)

以下代码将为您提供帮助:

的JavaScript:

function makeResponseBox() {
    document.getElementById("calculate").onclick = function()
    {
        var responseBox = document.createElement("DIV"); //create <div>
        var spanclose = document.createElement("span");
        var spantext = document.createTextNode("X");
        spanclose.appendChild(spantext);
        spanclose.setAttribute("class", "close" );
        spanclose.setAttribute("onclick", "removeDiv(this)" );
        responseBox.setAttribute("class", "content" );
        responseBox.appendChild(spanclose);
        document.getElementById('container').appendChild(responseBox);
    }
} // Close function (makeResponseBox)

window.onload = makeResponseBox;

function removeDiv(elem){
    elem.parentElement.remove()
}

的jQuery

function makeResponseBox() {
    document.getElementById("calculate").onclick = function()
    {
        var responseBox = document.createElement("DIV"); //create <div>
        var spanclose = document.createElement("span");
        var spantext = document.createTextNode("X");
        spanclose.appendChild(spantext);
        spanclose.setAttribute("class", "close" );
        responseBox.setAttribute("class", "content" );
        responseBox.appendChild(spanclose);
        document.getElementById('container').appendChild(responseBox);
    }
} // Close function (makeResponseBox)

window.onload = makeResponseBox;
$( "#container" ).on( "click",".close", function() {
    $(this).parent().remove();
});

答案 3 :(得分:0)

您可以使用此代码:

$("#container").on("click",".close",function(){
    $(this).closest(".content").hide()
})

这是演示代码:

&#13;
&#13;
<html>
    <title>Test Platform</title>

    <head>
        <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
   

    <style>
        body {
            margin: 0 0;
        }
        #container {
            border: 1px solid blue;
            white-space: nowrap;
            overflow: auto;
            font-size: 0;
        }

        #container > * {
            font-size: 8px;
            font-family: 'Open Sans', sans-serif;
        }

        #headerbar {
            font-size: 30px;
            color: white;
            padding-left: 10px;
            border: 1px solid darkgrey;
            height: 50px;
            background-color: darkslateblue;
        }
        #sidebar {
            display: inline-block;
            color: black;
            border: 1px solid darkgrey;
            width: 50px;
            height: 100vh;
            vertical-align: top;
            background-color: lightgrey;
            text-align: center;
            padding-top: 10px;
        }
        .content {
            display: inline-block;
            width: 200px;
            height: 100vh;
            border: 1px solid lightslategrey;
            margin: 0 1px 0 0;
            vertical-align: top;
            background-color: lightsteelblue;
        }
        .close {
            display: inline-block;
            padding: 2px 5px;
            background: #ededed;
            float: right;
        }
        .close:hover {
            float: right;
            display: inline-block;
            padding: 2px 5px;
            background: #ccc;
            color: #fff;
        }
    </style>
    </head>
    <body>
        <div id='container'>
            <div id='headerbar'>Header Div</div>
            <div id='sidebar'> <input type="button" value="O" id="calculate" />
                <br/><br/>
                <br/><br/>
            </div>
        </div>

        <script type='text/javascript'>
            $("#container").on("click",".close",function(){
                $(this).closest(".content").hide()
            })
        </script>

        <script>
            function makeResponseBox() {
                document.getElementById("calculate").onclick = function()
                {
                    var responseBox = document.createElement("DIV"); //create <div>
                    var spanclose = document.createElement("span");
                    var spantext = document.createTextNode("X");
                    spanclose.appendChild(spantext);
                    spanclose.setAttribute("class", "close" );
                    responseBox.setAttribute("class", "content" );
                    responseBox.appendChild(spanclose);
                    document.getElementById('container').appendChild(responseBox);
                }
            } // Close function (makeResponseBox)
            window.onload = makeResponseBox;
        </script>
    </body>
</html>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

当您运行函数以向类.close的所有元素添加关闭功能时,没有这样的元素。因为你还没有创建任何div。您应该在创建新div时添加onclick侦听器。

<html>
<title>Test Platform</title>

<head>
  <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>

<style>
  body {
    margin: 0 0;
  }
  #container {
    border: 1px solid blue;
    white-space: nowrap;
    overflow: auto;
    font-size: 0;
  }
  #container > * {
    font-size: 8px;
    font-family: 'Open Sans', sans-serif;
  }
  #headerbar {
    font-size: 30px;
    color: white;
    padding-left: 10px;
    border: 1px solid darkgrey;
    height: 50px;
    background-color: darkslateblue;
  }
  #sidebar {
    display: inline-block;
    color: black;
    border: 1px solid darkgrey;
    width: 50px;
    height: 100vh;
    vertical-align: top;
    background-color: lightgrey;
    text-align: center;
    padding-top: 10px;
  }
  .content {
    display: inline-block;
    width: 200px;
    height: 100vh;
    border: 1px solid lightslategrey;
    margin: 0 1px 0 0;
    vertical-align: top;
    background-color: lightsteelblue;
  }
  .close {
    display: inline-block;
    padding: 2px 5px;
    background: #ededed;
    float: right;
  }
  .close:hover {
    float: right;
    display: inline-block;
    padding: 2px 5px;
    background: #ccc;
    color: #fff;
  }
</style>

<body>

  <div id='container'>
    <div id='headerbar'>Header Div</div>
    <div id='sidebar'>
      <input type="button" value="O" id="calculate" />
      <br />
      <br />
      <br />
      <br />
    </div>

  </div>

  <script>
    function makeResponseBox() {
        document.getElementById("calculate").onclick = function() {
          var responseBox = document.createElement("DIV"); //create <div>
          var spanclose = document.createElement("span");
          var spantext = document.createTextNode("X");
          spanclose.appendChild(spantext);
          spanclose.setAttribute("class", "close");
          spanclose.onclick = function() {
            this.parentNode.parentNode
              .removeChild(this.parentNode);
            return false;
          };
          responseBox.setAttribute("class", "content");
          responseBox.appendChild(spanclose);
          document.getElementById('container').appendChild(responseBox);


        }
      } //close function (makeResponseBox)
    window.onload = makeResponseBox;
  </script>

</body>

</html>

答案 5 :(得分:0)

将此添加到makeResponseBox函数:

spanclose.onclick=function(){
this.parentNode.removeChild(this);
}

答案 6 :(得分:0)

您可以使用此代码:

<script type="text/javascript">
    $(document).ready(function() {
        $("body").on('click', '.close', function(e) {
            $(e.target.parentElement).remove();
        });
    });
</script>

答案 7 :(得分:0)

请参阅this fiddle

以下是更新的JavaScript代码。

function add() {
    var responseBox = document.createElement("DIV"); //create <div>
    var spanclose = document.createElement("span");
    var spantext = document.createTextNode("X");
    spanclose.appendChild(spantext);
    spanclose.setAttribute("class", "close");
    responseBox.setAttribute("class", "content");
    responseBox.setAttribute("onclick", "remove(this)");
    responseBox.appendChild(spanclose);
    document.getElementById('container').appendChild(responseBox);
}

function remove(elt) {
    elt.parentNode.removeChild(elt);
}