2013-07-06 77 views
1

我有一個多列(兩列div),從循環中檢索一些來自MySQL的信息。 (用戶名,小時,留言)。jQuery/AJAX刪除後留下白色空白空格里面多列div

+---------------------------+ +---------------------------+ 
| StackOverflow User  | | StackOverflow User2  | 
| 11:31 AM   X | | 11:37 AM    X | 
|       | |       | 
| Stack Overflow is a  | | Tags make it easy to find | 
| question and answer site | | interesting questions. | 
| for professional and  | | All questions are tagged | 
| enthusiast programmers. | | with their subject areas. | 
| It's built and run by you | | Each can have up to 5  | 
| as part of the Stack  | | tags, since a question | 
| Exchange network of Q&A | | might be related to  | 
| sites.     | | several subjects.   | 
|       | | Click any tag to see a | 
+---------------------------+ | list of questions with | 
           | that tag, or go to the | 
+---------------------------+ | tag list to browse for | 
| StackOverflow User2  | | topics that interest you. | 
| 11:56 AM    X | +---------------------------+ 
|       | 
| This post is not to  | +---------------------------+ 
| explain anything but just | | Stackoverflow User9  | 
| to show how the divs are | | 2 days ago   X | 
| are the current moment | |       | 
| and to describe their  | | Whoever sees this post | 
| issue that I'm unable  | | I appreciate for the help | 
| to know what is causing | | And who ever looks for | 
| if it's jQuery type of | | help in the same subject | 
| mansory or the Ajax.  | | I hope you find what you | 
+---------------------------+ | are looking for as it can | 
           | be tough to find something| 
           | that you're both needing | 
           | help with.. sometimes:) | 
           +---------------------------+ 

我現在的問題是,如果我按一下X按鈕刪除後,它不會完全刪除它,因爲我已經成立,但刪除後離開 高度盒子而不是底部框(被刪除的框下面的框)佔據空間並移動到其位置。問題如下:

單擊左側第一個帖子的刪除按鈕後。

       +---------------------------+ 
           | StackOverflow User2  | 
           | 11:37 AM    X | 
           |       | 
           | Tags make it easy to find | 
           | interesting questions. | 
           | All questions are tagged | 
           | with their subject areas. | 
           | Each can have up to 5  | 
           | tags, since a question | 
           | might be related to  | 
           | several subjects.   | 
           | Click any tag to see a | 
           | list of questions with | 
           | that tag, or go to the | 
+---------------------------+ | tag list to browse for | 
| StackOverflow User2  | | topics that interest you. | 
| 11:56 AM    X | +---------------------------+ 
|       | 
| This post is not to  | +---------------------------+ 
| explain anything but just | | Stackoverflow User9  | 
| to show how the divs are | | 2 days ago   X | 
| are the current moment | |       | 
| and to describe their  | | Whoever sees this post | 
| issue that I'm unable  | | I appreciate for the help | 
| to know what is causing | | And who ever looks for | 
| if it's jQuery type of | | help in the same subject | 
| mansory or the Ajax.  | | I hope you find what you | 
+---------------------------+ | are looking for as it can | 
           | be tough to find something| 
           | that you're both needing | 
           | help with.. sometimes:) | 
           +---------------------------+ 

讓元素的高度被刪除,而不是第二個帖子的高度降低,並將其放置。 我相信目前的問題與jQuery一樣,它是如何檢查元素的高度和自動調整,但我不確定,如果我刪除另一個 正確地上升。

這是我目前使用的代碼。

jQuery的多柱高度與它左右列調整的CSS>

$(document).ready(function() 
{ 
    var left_column_height = 0; 
    var right_column_height = 0; 
    var items = $('.item'); 

    for (var i = 0; i < items.length; i++) 
    { 
     if (left_column_height > right_column_height) 
     { 
      right_column_height+= items.eq(i).addClass('right').outerHeight(true); 
     } else { 
      left_column_height+= items.eq(i).outerHeight(true); 
     } 
    } 
}); 

.wrap { width: 100% } 
.wrap .item { width: 49%;float: left;clear: left; } 
.wrap .item.right { float: right;clear: right; } 

阿賈克斯/ jQuery的刪除帖子。 (可能不需要那麼我就做什麼,以顯示它是如何)

$(document).ready(function() 
{ 
    $('.postdelete').on("click",function() 
    { 
     var iD = $(this).attr("id"); 
     var dataString = 'post_iD='+ iD; 

     if(confirm("Sure you want to delete this update?")) 
     { 
      $.ajax(
      { 
       type: "POST", 
       url: "load_ajax/delete_message_ajax.php", // just passes a isset $_POST. 
       data: dataString, 
       cache: false, 
       success: function(html) 
       { 

        $("#stbody"+iD).slideUp()("slow",function(){ $("#stbody"+iD).remove().slideUp("slow"); }); 
       } 
      }); 
     } 
     return false; 
    }); 
}); 

的Html普通股利

<div class="wrap" id="php get id"> 
    <div class="item"> 
     <div class="box"> 
      <a>StackOverflow User</a> 
      <a>11:31 AM</a> 
      <a>MESSAGE</a> 
     </div> 
    </div> 
</div> 

從什麼我知道,我只是「數字」,這似乎更加明顯該問題肯定來自jQuery多列列表或AJAX帖子。 我明白那裏有Mansonry和其他一些腳本,但是我會選擇較小的代碼來完成同樣的事情,再加上我只會在這裏使用它,所以我寧願堅持一個小的一段代碼比移動到更大的一個並導致服務器壓力。

UPDATE 2:經過測試,我相信問題出在jQuery上,因爲它只讀取.right而不是.left,所以它只刪除空白空間,如果我刪除了正確的。

+0

我覺得你有一個額外的'()'第一'slideUp'在'$後(「#stbody」+ iD).slideUp()(「slow」,function(){$(「#stbody」+ iD).remove()。slideUp(「slow」);});'。嘗試刪除 - 「$(」#stbody「+ iD).slideUp(」slow「,function(){$(」#stbody「+ iD).remove()。slideUp(」slow「);});' – Sean

+0

你是對的,我確實有一個額外的(),但它仍然沒有改變這個問題! – iBrazilian

回答

2

第一個:不要使用$('#id'),只有$('。class')用於$(this)來限制元素的事件,(#id爲多個動作)。

要在jQuery的一個循環:

var items = $('.item'); 
items.each(function({ 
    ... 
}); // not for(...){} with .lenght.... 

但它不是爲你一個很好的解決方案...

你爲什麼不使用.height()有高度你專欄?

HLeft = $('.column.left').height(); 
HRight = $('.column.right').height(); 

隱藏塊:

$('.postdelete').on("click",function() { 

    par=$(this).parents('.item'); 
    dataString = par.serialize('input'); 

... ajax sucess ... 

     par.slideUp("slow",function(){ 
      $(this).remove(); 
     }); 

HTML(不要忘記連載輸入)

<div class="column"> 
    <div class="item"> 
     <input type="hidden" name="post_id" value="myId" /> 
     <div class="box"> 
      <a>StackOverflow User</a> 
      <a>11:31 AM</a> 
      <a>MESSAGE</a> 
     </div> 
    </div> 
</div> 

對於Style刪除:

clear:left; 
clear:right; 

使用這樣的:

.column  { width:50%; height:auto; margin:0; padding:0; } 
.column.left { float:left;} 
.column.right { float:right;} 
.item   { width:100%; height:auto; float:left; } 

再見

+1

完美,從你的答案中學到很多,我非常感謝。 – iBrazilian

1

你的問題是由箱卸下的一個下DOM的位置造成的。

這裏是一個快速和骯髒的黑客:http://jsfiddle.net/W3WEr/6/

$('.postdelete').on("click",function() 
{ 
    item = $(this).parents('div.item'); 
    item.slideUp("slow",function(){ 
     // Hack Dom position 
     item.before($('#last')); 

     item.remove(); 
    }); 
    return false; 
}); 

當您刪除左邊的第一個塊,你需要改變每個節點的位置,它(所以浮動風格的工作方式後,他們不得不)。

簡而言之,在您的ajax請求之後,您必須掃描每個節點並將其按照正確的順序排列在dom中。

當然,這只是一個快速的黑客攻擊,但我認爲它可以讓你解決這個問題。

哦!順便說一句:效果基本show( 「慢」),不效果基本show()( 「慢」 ......)