2017-09-25 96 views
0

我试图使用jQuery如何使用jQuery删除下一个元素的内容?

$('#deleteAssocPesident').click(function(e){ 
    // e.preventDefault(); 
    console.log(e); 

    $(this).next('div').next('#deletePresidentBlock').html(''); 
}); 

的Html

<div class="user_president_designation" id="user_designation_president">President <i class="fa fa-trash-o" aria-hidden="true" id="deleteAssocPesident" style="float: right;font-size: 22px;margin-top: 4px;" ></i></div> 
<div class="user_president_block" id="deletePresidentBlock"> 
    <img src="" style="max-width: 300px;max-height: 300px;min-height: 300px;" class="img img-responsive" /> 
    <div class="user_president_name">{{$firstName}}&nbsp;{{$lastName}}</div> 
    <input type="hidden" value="" name="president"> 
</div> 

我试过很多方法,但它不工作。我相信,这功能在工作这可能是问题清空下一格的HTML数据能找到的div 下一个倾斜的任何帮助appriciated

更新

我有很多相似块,所以我不能使用的ID。所以我有空最近删除的div块

我现在已经添加的类名deletePresidentBlock因为ID应该是unque

+2

既然你在球场上有一个ID,为什么不只是'$( '#deletePresidentBlock')HTML( '')' – Cfreak

+1

[下一页](HTTPS:// API。 jquery.com/next/)*获取匹配元素集中每个元素的紧随其后的兄弟。如果提供了一个选择器,只有当它匹配该选择器时才会检索下一个兄弟。*。如果您试图引用一个ID,那么只需在该ID上清空,因为它们是** unique **。 –

+0

@ Cfreak.i有很多块,所以如果添加像它会删除all.i想要remvoe特定的bock用户点击 – vision

回答

1

在这里,你去了一个解决方案

$('#deleteAssocPesident').click(function(e){ 
 
    $(this).closest('div#user_designation_president').next('#deletePresidentBlock').html(''); 
 
});
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="user_president_designation" id="user_designation_president">President <i class="fa fa-trash-o" aria-hidden="true" id="deleteAssocPesident" style="float: right;font-size: 22px;margin-top: 4px;" ></i> 
 
</div> 
 
<div class="user_president_block" id="deletePresidentBlock"> 
 
    <img src="" style="max-width: 300px;max-height: 300px;min-height: 300px;" class="img img-responsive" /> 
 
    <div class="user_president_name">{{$firstName}}&nbsp;{{$lastName}}</div> 
 
    <input type="hidden" value="" name="president"> 
 
</div>

希望这会对你有帮助。

+0

@ Shiladitya.thanks很多.works像魅力 – vision

+0

@vision欢迎好友:) – Shiladitya

+0

第一集团kdiv删除推进。但不是其他分区 – vision

0
$('#deleteAssocPesident').click(function(e){ 
    // e.preventDefault(); 
    console.log(e); 

    //$('#deletePresidentBlock').html(''); 
    $(this).parent().next('div').children('#deletePresidentBlock').html(''); 
}); 
1

试试这个:

$('#deleteAssocPesident').click(function(e) { 
    $(this).parent().siblings('div#deletePresidentBlock:first').html(''); 
}); 
+0

你不需要:首先如果你有唯一的ID。这是当你有类而不是ID – kheya

+0

@ kheya.thanks的答案 – vision

相关问题