简体   繁体   English

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

[英]How can I remove the contents of the next element with jQuery?

I am trying to empty next div html data using jquery 我正在尝试使用jQuery清空下一个div html数据

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

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

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>

I have tried many methods but its not working .I am sure this function is working it might be the issue of next cant able to find div Any help is appriciated 我已经尝试了很多方法,但是它不起作用。我确定此功能正在起作用,这可能是下一个无法找到div的问题。

Updated 更新

I have many similar block so i cant use id .so i have empty closest delete div block 我有很多类似的块,所以我不能使用id。所以我有空的最近删除div块

Now i have added class name deletePresidentBlock since id should be unque 现在我添加了类名deletePresidentBlock因为id应该是唯一的

Here you go with a solution 在这里你有一个解决方案

 $('#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> 

Hope this will help you. 希望这会帮助你。

Try this: 尝试这个:

$('#deleteAssocPesident').click(function(e) {
    $(this).parent().siblings('div#deletePresidentBlock:first').html('');
});
$('#deleteAssocPesident').click(function(e){
  // e.preventDefault();
  console.log(e);

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

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

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