简体   繁体   English

点击链接删除Div

[英]Remove Div on link click

I am doing a code in which I want to delete a div when its inner link("Delete") is clicked. 我正在执行一个代码,当单击它的内部链接(“删除”)时,我想删除一个div。 I know this question might be repeating and I have tried so many methods but not working. 我知道这个问题可能会重复,并且我尝试了很多方法但没有用。 I dont know what's the problem. 我不知道这是什么问题。

Here is my HTML Rendering Code: 这是我的HTML渲染代码:

<div id="ViewRows">
    <br>
    <div class="ViewRow"> NOS: FA Comment: finance
        <a class="deleteViewRow" href="#">Delete</a>
    <br>
    </div>
    <div class="ViewRow">
       NOS: TPA Comment: Assistance
       <a class="deleteViewRow" href="#">Delete</a>
       <br>
    </div>
</div>

Here is my javascript Code for removal of that div. 这是我删除该div的javascript代码。

$("a.deleteViewRow").live("click", function () {
   $(this).parents("div.ViewRow:first").remove();
   return false;
});

I also tried following javascript: 我也尝试过以下javascript:

$("#ViewRows").on("click", "a.deleteViewRow", function () {
    $(this).closest("div.ViewRow").andSelf().remove();
    return false;
});

I have tried same code on another page. 我在另一页上尝试了相同的代码。 It is working but when I applied the same logic in another page. 它正在工作,但是当我在另一页中应用相同的逻辑时。 It's not working. 没用 I know you all are right but I dont know whats problem. 我知道你们都是对的,但我不知道出什么问题。 In firebug it's not even going into the function. 在萤火虫中,它甚至都没有进入功能。

This should work: 这应该工作:

$("#ViewRows").on("click", "a.deleteViewRow", function (e) {
    // prevent browser from following link
    e.preventDefault();
    // ".andSelf()" can be skipped - once the parent div is removed, the link gets removed as well
    $(this).closest("div.ViewRow").remove();
});

this simple yet efficient code works fine: http://jsfiddle.net/L2CsH/ 这个简单而有效的代码可以正常工作: http : //jsfiddle.net/L2CsH/

$("#ViewRows").on("click", "a.deleteViewRow", function () {
    $(this).parent().remove();
});

you need to prevent default action of link. 您需要阻止链接的默认操作。 event.preventDefault()

$("#ViewRows").on("click", "a.deleteViewRow", function (e) {
    e.preventDefault();
    $(this).closest("div.ViewRow").remove(); //.andSelf() is not needed here. 
    //return false;
});

Demo : Remove Div JSFiddle 演示: 删除Div JSFiddle

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

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