Javascript Length返回兄弟姐妹和韩元总数的值';如果删除了同级,则不会更新。为什么?
当页面第一次加载时,同级上的.length返回3。然后一个“点击”事件触发一个post,成功后删除一个同级,再次调用.length,但它一直返回3 请注意,每个兄弟节点都是在随机时间通过“单击”事件删除的,而不是在同一个循环中 示例:(此处手动编码,但应解释行为)Javascript Length返回兄弟姐妹和韩元总数的值';如果删除了同级,则不会更新。为什么?,javascript,jquery,Javascript,Jquery,当页面第一次加载时,同级上的.length返回3。然后一个“点击”事件触发一个post,成功后删除一个同级,再次调用.length,但它一直返回3 请注意,每个兄弟节点都是在随机时间通过“单击”事件删除的,而不是在同一个循环中 示例:(此处手动编码,但应解释行为) $(文档).ready(函数(){ var total_li=$('#un33k').children().length;//3(仅限测试) $('a.clicked')。单击(函数(){ var me=这个; $.pos
$(文档).ready(函数(){
var total_li=$('#un33k').children().length;//3(仅限测试)
$('a.clicked')。单击(函数(){
var me=这个;
$.post('somelink',{…},函数(数据,状态){//假设成功
var new_total_li=$(me).closest('li').sides().length;//3,很好
var parent=$(me).closest('li').parent()//无法硬编码id
$(me).最近的('li').remove();//假设单击的li现在已消失
new_total_li=$(parent).children().length;//3,但现在应该是2?
}“json”);
});
});
即使点击几分钟后,仍然会显示3。为什么?
更新:在此处复制实际代码:
这一行是罪魁祸首(console.log($(me).closest($(li').sibles().length),它显示相同的初始编号,无论删除了多少个同级
<script>
$(document).ready(function(){
$('a.get2post').click(function () {
var me = this;
var url = $(me).attr('href');
if ($(me).hasClass('confirm')){
if(!window.confirm("Are you sure?")){
return false;
}
}
if ($(me).hasClass('deletion')){
var postdata = {ajax: 'true' };
$.post(url, postdata, function(data, status) {
if (status == 'success'){
$(me).closest('li').hide('slow', function(){ $(me).remove(); });
console.log($(me).closest('li').siblings().length) // problem
}
}, "json");
}
return false;
});
});
</script>
$(文档).ready(函数(){
$('a.get2post')。单击(函数(){
var me=这个;
var url=$(me.attr('href');
if($(me).hasClass('confirm')){
如果(!window.confirm(“确定吗?”)){
返回false;
}
}
if($(me).hasClass('deletation')){
var postdata={ajax:'true'};
$.post(url、postdata、函数(数据、状态){
如果(状态=‘成功’){
$(me.nexist('li').hide('slow',function(){$(me.remove();});
console.log($(me).closest('li').sides().length)//问题
}
}“json”);
}
返回false;
});
});
您的问题是$(me).remove();
行。这就是删除
隐藏完成后,它实际上会删除目标
元素。$(me).closest('li').sides().length;
应返回两个,而不是三个,这是同级列表项的数量,而不是列表项的总数。但即使出现此错误,$(parent).children().length
为我返回两个。这是一个。但它对我有效。效果很好。您的链接是否会将您从页面中带走?谢谢各位,是的,回头看代码,我发现实际删除是在成功的$.post块中发生的。这有关系吗?我希望我可以将代码复制并粘贴到这里…但这是j唯一需要做的事情现在就向我吐。thx。@ValNeekman这绝对有关系。$.post
发送一个异步请求,因此响应将/可能在将来某个不确定的时间发生,而代码现在正在运行…现在。所以现在的代码不知道将来会发生什么。您可以将代码粘贴在这里…编辑您的问题并将它附加到末尾。你100%正确,是的,我确实需要删除它。谢谢。
<script>
$(document).ready(function(){
$('a.get2post').click(function () {
var me = this;
var url = $(me).attr('href');
if ($(me).hasClass('confirm')){
if(!window.confirm("Are you sure?")){
return false;
}
}
if ($(me).hasClass('deletion')){
var postdata = {ajax: 'true' };
$.post(url, postdata, function(data, status) {
if (status == 'success'){
$(me).closest('li').hide('slow', function(){ $(me).remove(); });
console.log($(me).closest('li').siblings().length) // problem
}
}, "json");
}
return false;
});
});
</script>
$(document).ready(function(){
$('a.get2post').click(function () {
var me = $(this);
var parent_li = me.closest("li");
var url = "/echo/json/";//$(me).attr('href');
if (me.hasClass('confirm')){
if(!window.confirm("Are you sure?")){
return false;
}
}
if (me.hasClass('deletion')){
var postdata = {stuff: 'stuff' };
$.post(url, postdata, function(data, status) {
if (status == 'success'){
parent_li.hide('slow', function () {
$(this).remove();
});
console.log(parent_li.siblings().length) // problem
}
}, "json");
}
return false;
});
});