简体   繁体   English

删除后jQuery / AJAX在多列div中留下白色空格

[英]jQuery/AJAX after post deletion leaves white blank space inside Multi-Column div

I've got a multi-column(two column div) with a loop retrieving some information from MySQL. 我有一个多列(两列div),循环从MySQL中检索一些信息。 (Username, Hour, Message). (用户名,小时,消息)。

+---------------------------+   +---------------------------+
|  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:)   |
                                +---------------------------+

My current issue is that, If I click on the X button to delete the post, It does delete it perfectly as I have set up, but after deletion it leaves the height of the box instead of the bottom box(box below the one deleted) taking it's space and moving to it's position. 我当前的问题是,如果我点击X按钮删除帖子,它确实删除它完全按照我的设置,但删除后它保留框的高度而不是底部框(删除一个框下方的框)抓住它的空间并移动到它的位置。 The issue as follows: 问题如下:

After clicking the delete button for the first post on the left. 单击左侧第一个帖子的删除按钮后。

                                +---------------------------+
                                | 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:)   |
                                +---------------------------+

Leaves the height of the element deleted instead of the Second post below going up and taking it's place. 留下元素的高度而不是下面的第二个帖子上升并取而代之。 I believe the current issue stands with the jQuery of how it's checking for the elements height and auto adjusting but I'm unsure as if I delete another on the right it goes up correctly. 我相信当前的问题与jQuery如何检查元素高度和自动调整有关,但我不确定好像我在右边删除另一个它正确上升。

Here is the code I'm currently using. 这是我目前正在使用的代码。

jQuery multi-column height adjustment with left and right columns with it's CSS> 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; }

Ajax/jQuery for deleting the posts. 用于删除帖子的Ajax / 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 Normal Div(probably not needed so I'll just make something to show how it is) Html Normal Div(可能不需要,所以我只是做一些事情来展示它是怎样的)

<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>

From what I know I just "figure" which seems to be more obvious that the issue is definetly coming from either the jQuery multi-column list or the AJAX post. 据我所知,我只是“想象”,这个问题似乎更明显,这个问题肯定来自jQuery多列列表或AJAX帖子。 I understand there is Mansonry and a couple of other scripts out there, but I'm someone who will choose smaller pieces of code to accomplish the same thing, plus I'll only use this here so I'd rather stick with a small piece of code than move to a larger one and cause stress on the server. 我知道有Mansonry和其他一些脚本,但是我会选择较小的代码来完成同样的事情,而且我只会在这里使用它,所以我宁愿坚持一小块代码而不是移动到更大的代码并导致服务器上的压力。

UPDATE 2 : After testing I believe the issue is in the jQuery because it only reads a .right and not .left so it only deletes the blank white space if I delete the right one as well. 更新2 :测试后我认为问题出在jQuery中,因为它只读取.right而不是.left,所以如果我删除了正确的空格,它只会删除空白空格。

First : Don't use $('#id'), just $('.class') for the event with $(this) to scope the element, (#id make some problems for multiple action). 第一:不要使用$('#id'),只使用$('。class')来使用$(this)来调整元素的范围,(#id为多个操作带来一些问题)。

To make a loop in jquery : 在jquery中创建一个循环:

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

But its not a good solution for you ... 但它对你来说不是一个好的解决方案......

why don't you use .height() to have the height of you column ? 为什么不使用.height()来获得列的高度?

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

to hide block : 隐藏块:

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

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

... ajax sucess ...

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

Html (dont forget the input for serialize) 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>

For the style remove : 对于样式删除:

clear:left;
clear:right;

Use something like this : 使用这样的东西:

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

See ya 再见

Your problem is caused by the dom position of the box under the one you removed. 您的问题是由您移除的盒子下面的盒子的dom位置引起的。

Here is a quick and dirty hack : http://jsfiddle.net/W3WEr/6/ 这是一个快速而肮脏的黑客: 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;
});

When you remove the first block on the left, you need to change the position of each node after it (so the float styles works the way they have to). 当你删除左边的第一个块时,你需要改变它后面每个节点的位置(所以浮动样式按照它们的方式工作)。

In short, after your ajax request you have to scan each nodes and place it in the right order in the dom. 简而言之,在您的ajax请求之后,您必须扫描每个节点并将其按顺序放在dom中。

Of course, it's just a quick hack, but i think it gives you the way to work this out. 当然,这只是一个快速的黑客,但我认为它为你提供了解决这个问题的方法。

Oh ! 哦! and by the way : slideUp("slow") , not slideUp()("slow"...) 顺便说一句: slideUp(“慢”) ,而不是slideUp()(“慢”......)

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

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