我知道有很多话题。在这个主题中主要是关于除了一个元素之外的删除,但它的孩子也正在删除。
所以我拥有:
// Both side
TelnetUserConnections = new Meteor.Collection('TelnetUserConnections');
// Client side
Meteor.startup(function(){
// Timer who refresh the heartbeat on server
Meteor.setInterval(function () {
Meteor.call('keepAlive', Blaze._globalHelpers.currentUserId());
}, 5000);
});
// Server side
Meteor.methods({
"keepAlive": function (user_id) {
if (!TelnetUserConnections.findOne({user_id: user_id})) {
TelnetUserConnections.insert({user_id: user_id});
}
TelnetUserConnections.update({user_id: user_id}, {$set: {last_seen: (new Date()).getTime()}});
}
});
Meteor.setInterval(function () {
// do the job when user hasn't pinged since 20 secondes
var now = (new Date()).getTime();
TelnetUserConnections.find({last_seen: {$lt: (now - 20 * 1000)}}).forEach(function (user) {
/* here is my destroy stuff */
TelnetUserConnections.remove({_id : user._id});
});
});
我正在尝试使用Jquery:
<div id='maindiv'>
<div id='dont want to remove'>
<div class= "don't want to remove"></div>
<div class= "dont want to remove"></div>
</div>
<div id="want to delete"></div>
</div>
当我这样做时,我没有任何孩子。
$('#maindiv').find('*:not("#dont want to remove")').remove();
感谢您的关注。祝你有愉快的一天。
答案 0 :(得分:1)
你可以使用:
$('#maindiv').children(':not(.dont_want_to_remove)').remove();
我使用孩子而非查找,查找是递归的,这就是删除内部元素的原因
请注意我使用的课程&#34; dont_want_to_remove&#34; (或者你可以坚持使用ID,如果你愿意的话)没有空格!
答案 1 :(得分:1)
使用选择器很难(如果不是不可能)。
相反,您可以添加&#34; dontremove&#34;对元素及其所有元素进行类,然后删除所有不具有该类的元素:
//add dontremove class to ID and all its descendants:
$('#dontwanttoremove, #dontwanttoremove *').addClass('dontremove');
//remove all elements without the dontremove class
$('#maindiv :not(.dontremove)').remove();
//clean up
$('.dontremove').removeClass('dontremove');
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='maindiv'>
<div id='dontwanttoremove'>
<div>Keep me 1</div>
<div>Keep me 2</div>
Keep me 3
</div>
<div id="wanttodelete">Delete me</div>
</div>
&#13;