簡體   English   中英

按鈕單擊時刪除div

[英]delete div on button click

我試圖刪除div按鈕單擊,但問題是我需要按鈕刪除它包含的div而不將div的id傳遞給刪除按鈕

所以最后這段代碼應該能夠刪除div而不傳遞div的id,而是依賴於傳遞this引用

<!doctype html>
<html>

<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $(document).on('mouseenter', '#divbutton', function() {
                $(this).find(":button").show();
            }).on('mouseleave', '#divbutton', function() {
                $(this).find(":button").hide();
            });
        });
    </script>

    <style>
        #divbutton {
            height: 100px;
            background: #0000ff;
        }

        #divbutton2 {
            height: 100px;
            background: #0000ff;
        }

        #divbutton3 {
            height: 100px;
            background: #0000ff;
        }
    </style>
</head>
<body>
    <div id="divbutton">
        <button type="button" style="display: none;" id="i" onclick="document.body.removeChild(document.getElementById('divbutton'))">Hello</button>
    </div>

    <div id="divbutton2">
        <button type="button" style="display: none;" id="i" onclick="document.body.removeChild(document.getElementById('divbutton2'))">Hello </button>
    </div>

    <div id="divbutton3">
        <button type="button" style="display: none;" id="i" onclick="document.body.removeChild(document.getElementById('divbutton3'))">Hello </button>
    </div>
</body>

</html>

如果你想知道如何使用this引用來獲取父元素以將它傳遞給removeChild ,那么它很容易,只需使用parentNode

<div class="divbutton">
    <button type="button" style="display: none;" id="i" onclick="document.body.removeChild(this.parentNode)">Hello</button>
</div>

但是,既然你正在使用jQuery,那么利用它的功能是有意義的:

$(document).on('mouseenter', '.divbutton', function () {
    $(this).find(":button").show();
}).on('mouseleave', '.divbutton', function () {
    $(this).find(":button").hide();
}).on('click', ':button', function() {
    $(this).parent().remove();
});

我還將id #divbuttonX更改為類名.divbutton ,在這種情況下CSS變得更簡單。

演示: http //jsfiddle.net/5qfjo0c7/

您可以使用.closest刪除div

  $(document).ready(function () { $(document).on('mouseenter', 'div[id^=divbutton]', function () { $(this).find(":button").show(); }).on('mouseleave', 'div[id^=divbutton]', function () { $(this).find(":button").hide(); }); $(document).on('click', 'button#i', function () { $(this).closest("div").remove(); }); }); 
 #divbutton { height: 100px; background: #0000ff; } #divbutton2 { height: 100px; background: #0000ff; } #divbutton3 { height: 100px; background: #0000ff; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="divbutton"> <button type="button" style="display: none;" id="i" >Hello</button> </div> <div id="divbutton2" > <button type="button" style="display: none;" id="i">Hello </button> </div> <div id="divbutton3" > <button type="button" style="display: none;" id="i">Hello </button> </div> 

試試這會有所幫助:

。$(本).parent()最接近( 'DIV')隱藏();

$("button").click(function () {
    $(this).parent().closest('div').hide();
});

演示:

 $("button").click(function () { $(this).parent().closest('div').hide(); }); 
 #divbutton { background:powderblue; padding:10px; height:100px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="divbutton"> <button id="i">Hello</button> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM