简体   繁体   English

如何在父结构中删除div?

[英]How to remove a div in the parent structure?

I am trying to remove some elements using a link. 我正在尝试使用链接删除一些元素。 The elements that I am trying to remove are in a link's parent div, but i cannot seem to remove them. 我要删除的元素在链接的父div中,但是我似乎无法删除它们。

Here is the HTML: 这是HTML:

                  <div class="QR-answer-holder">
                  <label class="QR-answer-label">Enter an answer:</label>
                  <input class="QR-answer-input textbox" type="text" name="answer" />
              </div>
                <a class="new-answer new-qr-answer-space" href="javascript:void(0)">New answer space</a> | 
                <a class="remove-answer remove-qr-answer-space" href="javascript:void(0)">Remove Answer Space</a>

Here is the JQuery: 这是JQuery:

      $remove_qr_answer = $(".remove-qr-answer-space");
  $remove_qr_answer.live("click", function() {
      $(this).parent.$(".QR-answer-holder").$(".QR-answer-label").remove();
      $(this).parent.$(".QR-answer-holder").$(".QR-answer-input").remove();

  });

Is there anyway to make it so the remove button removes the label and input closest to the end of the div? 无论如何,要使用删除按钮来删除标签和输入最接近div末尾的输入吗? (or does it do that automatically)? (或自动执行此操作)?

You're accessing the .parent() node from that anchor .new-qr-answer-space . 您正在从该anchor .new-qr-answer-space访问.parent()节点。

Infact, you need to get the .sibling() , since the div.QR-answer-holder is not the parent node: 实际上,您需要获取.sibling() ,因为div.QR-answer-holder 不是父节点:

$remove_qr_answer = $(".remove-qr-answer-space");
$remove_qr_answer.live("click", function() {
    $(this).siblings(".QR-answer-holder").find(".QR-answer-label:last, .QR-answer-input:last").remove();
});

parent is a method, not a property. parent是一种方法,而不是属性。 There is no $ method in the jQuery object, you use the find method to locate children: jQuery对象中没有$方法,您可以使用find方法查找子级:

$(this).parent().find(".QR-answer-holder .QR-answer-label").remove();
$(this).parent().find(".QR-answer-holder .QR-answer-input").remove();

Edit: 编辑:

As you actually want to get the closest sibling before the link, you should use the prev method instead: 由于您实际上想获得链接之前最接近的兄弟姐妹,因此应该改用prev方法:

$(this).prev().find(".QR-answer-holder .QR-answer-label").remove();
$(this).prev().find(".QR-answer-holder .QR-answer-input").remove();

Or if you want to remove all elements in the div, simply: 或者,如果您要删除div中的所有元素,只需:

$(this).prev().empty();

Try using this 试试这个

HTML: HTML:

<div class="QR-answer-holder">
    <label class="QR-answer-label">Enter an answer:</label>
    <input class="QR-answer-input textbox" type="text" name="answer" />
</div>
<a class="new-answer new-qr-answer-space" href="#">New answer space</a> | 
<a class="remove-answer remove-qr-answer-space" href="#">Remove Answer Space</a>

JavaScript: JavaScript:

$remove_qr_answer = $(".remove-qr-answer-space");
$remove_qr_answer.live("click", function(e) {
    e.preventDefault();
    $(this).siblings(".QR-answer-holder").find(".QR-answer-label,.QR-answer-input").remove();
});

try 尝试

$remove_qr_answer = $(".remove-qr-answer-space");   
$remove_qr_answer.live("click", function() {
       $(this).parent().find($(".QR-answer-holder").remove();        
}); 

and it should remove the lable and input as those are inside the placeholder 它应该删除标签和输入,因为它们位于占位符内

see siblings : 兄弟姐妹

$(this).siblings('.QR-answer-holder .QR-answer-label').remove();

this would remove the elements '.QR-answer-holder .QR-answer-label' from the same dom node as this. 这将从与此相同的dom节点中删除元素“ .QR-answer-holder .QR-answer-label”。

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

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