[英]Remove DOM node from a list by clicking (x) button element
I am dynamically adding this value on my page.我在我的页面上动态添加这个值。 and the code is:
代码是:
function favJobs(data){
number_of_jobs_applied=data.total_bookmarked;
$('.bookmark-title').append(number_of_jobs_applied," Job Bookmarked");
for(index=0;index<6;index++){
var dateString = data.bookmarked_jobs[index].application_deadline;
var momentObj = moment(dateString, 'YYYY-MM-DD');
var dateMomentString = momentObj.format('MMM DD, YYYY');
if (dateMomentString=='Invalid date'){
var dateMomentString = "n/a";
}
var fav_jobs_html = '<div class="job-list" id="'+data.bookmarked_jobs[index].job_id+'">'+
'<div class="thumb">'+
'<a href="#">'+
'<img src="'+data.bookmarked_jobs[index].profile_picture+'" style="max-width:70px"; class="img-fluid" alt="">'+
'</a>'+
'</div>'+
'<div class="body">'+
'<div class="content">'+
'<h4><a href="/job-detail/'+ data.bookmarked_jobs[index].slug+'">'+data.bookmarked_jobs[index].title+'</a></h4>'+
'<div class="info">'+
'<span class="company"><a href="#"><i class="fas fa-building" style="margin-right: 5px"></i>'+data.bookmarked_jobs[index].company_name+'</a></span>'+
'<span class="office-location"><a href="#"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-map-pin"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"></path><circle cx="12" cy="10" r="3"></circle></svg>'+data.bookmarked_jobs[index].job_location+'</a></span>'+
'<span class="job-type full-time"><a href="#"><img style="margin-right: 6px; vertical-align: top; padding-top: 5px;" src="/static/images/img_clock.png" width="12">'+data.bookmarked_jobs[index].employment_status+'</a></span>'+
'</div>'+
'</div>'+
'<div class="more">'+
'<div class="buttons">'+
'<a href="#" class="button">Apply Now</a>'+
'<a href="#" class="favourite"><i data-feather="heart"></i></a>'+
'</div>'+
'<a href="#" class="bookmark-remove" onclick="myFunction(this.id)"><i class="fas fa-times"></i></a>'+
'<p class="deadline">Deadline:'+dateMomentString+'</p>'+
'</div>'+
'</div>'+
'</div>';
$('.bookmark-area').append(fav_jobs_html);
}
}
I want to delete when the cross section will clicked.我想在单击横截面时删除。 But I can't able to delete it.
但我无法删除它。 -_-
-_-
So what i wrote:所以我写的是:
function myFunction(e) {
$(this).parent().remove();
alert("Deleted");
}
It just shows Deleted alert but why not deleting anything.它只显示已删除警报,但为什么不删除任何内容。
How to do it.怎么做。 Please help
请帮忙
this
to your myFunction
function.this
传递给您的myFunction
function。 Currently you are passing:目前您正在通过:
onclick="myFunction(this.id)"
But you should pass a reference to the element clicked, so you can do:但是您应该传递对单击的元素的引用,因此您可以这样做:
onclick="myFunction(this)"
and then:接着:
function myFunction( elm ) {
$(elm).closest('.job-list').remove() // safer to use "closest" than "parent"
}
function myFunction(elm){ $(elm).closest('.job-list').remove() }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul> <li class='job-list'> <p>item 1</p> <div> <button onclick="myFunction(this)">×</button> </div> </li> <li class='job-list'> <p>item 2</p> <div> <button onclick="myFunction(this)">×</button> </div> </li> <li class='job-list'> <p>item 3</p> <div> <button onclick="myFunction(this)">×</button> </div> </li> </ul>
To debug such scenarios you should use console.log
instead of alert
and then you could have meaningful logs, such as console.log(this)
to see if this
is a the DOME node you want or is it something else.要调试此类场景,您应该使用
console.log
而不是alert
,然后您可以获得有意义的日志,例如console.log(this)
以查看this
是您想要的 DOME节点还是其他东西。
I would go with:我会 go 与:
function myFunction(e) {
$(this).closest('.job-list').remove();
alert("Deleted");
}
You have to check the position of your click event (which DOM element is clicked) and then go upwards until you reach the main container (eg job-list
).您必须检查单击事件的 position(单击了哪个 DOM 元素),然后向上检查 go 直到到达主容器(例如
job-list
)。 Hope this helps and I did read the HTML correctly.希望这会有所帮助,我确实正确阅读了 HTML。
I think you want to delete whole job-list
class div on the bookmark-remove
link click.我认为您想在
bookmark-remove
链接单击上删除整个job-list
class div。 To do that you can use .closest()
method instead.为此,您可以改用
.closest()
方法。
Firstly, update your HTML string like:首先,更新您的 HTML 字符串,如:
'<a href="#" class="bookmark-remove" onclick="myFunction(this)"><i class="fas fa-times"></i></a>'+
So, here instead on passing the this.id
, we want to pass the current dom element to the clcik event handler like myFunction(this)
and then update the function like:因此,在这里,在传递
this.id
时,我们希望将当前 dom 元素传递给 clcik 事件处理程序,如myFunction(this)
,然后更新 function ,如:
function myFunction(obj) {
$(obj).closest('.job-list').remove();
alert("Deleted");
}
Also, it's generally not a good idea to use inline event handlers.此外,使用内联事件处理程序通常不是一个好主意。 So, you can use Event Delegation here instead like:
因此,您可以在此处使用事件委托,例如:
Remove the onclick
attribute from HTML string first and just keep it:首先从 HTML 字符串中删除
onclick
属性并保留它:
'<a href="#" class="bookmark-remove"><i class="fas fa-times"></i></a>'+...
Then attach a delegated event handler like:然后附加一个委托事件处理程序,如:
$('.bookmark-area').on("click", ".bookmark-remove", function(event) { event.preventDefault(); $(this).closest('.job-list').remove() });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.