为什么alert / console.log使用Jquery Drag& amp;下降

时间:2017-03-14 11:36:57

标签: javascript jquery alert console.log

我有以下HTML结构:

<div id="table_wrapper">
<div class="rows_table" id="rows_table_row_0">
    <div class="rows_table_row highlight-row-0" id="row_0">
        <div class="rows_table_cell rows_table_cell_small">row</div>
        <div class="rows_table_cell rows_table_cell_small">0</div>
        <div class="rows_table_cell rows_table_cell_small">TR</div>
        <div class="rows_table_cell rows_table_cell_big">sujet ligne_booleen cBackCouleurTab3</div>
        <div class="rows_table_cell rows_table_cell_button"><button id="remove-row-0" class="button_remove_row"><img src="remove_row-25.png"></button></div>
        <div class="rows_table_cell rows_table_cell_button"><button id="select-row-0-col" class="button_select_col"><img src="select_col-25.png"></button></div>
    </div>
</div>
<div class="cols_table" id="cols_table_row_0">
    <div class="cols_table_body" id="cols_table_row_0_body">
        <div class="cols_table_row" draggable="true" id="col_0">
            <div class="cols_table_cell cols_table_cell_small">col</div>
            <div class="cols_table_cell cols_table_cell_small">0</div>
            <div class="cols_table_cell cols_table_cell_small">TD</div>
            <div class="cols_table_cell cols_table_cell_middle">sujetCase3</div>
            <div class="cols_table_cell cols_table_cell_middle">ghj</div>
            <div class="cols_table_cell cols_table_cell_middle">false</div>
            <div class="cols_table_cell cols_table_cell_button"><button id="remove-col-0" class="button_remove_col"><img src="remove_col-25.png"></button></div>
        </div>
        <div class="cols_table_row" draggable="true" id="col_1">
            <div class="cols_table_cell cols_table_cell_small">col</div>
            <div class="cols_table_cell cols_table_cell_small">1</div>
            <div class="cols_table_cell cols_table_cell_small">TD</div>
            <div class="cols_table_cell cols_table_cell_middle">sujetCase6 cBackCouleurTab4</div>
            <div class="cols_table_cell cols_table_cell_middle">fghj</div>
            <div class="cols_table_cell cols_table_cell_middle">false</div>
            <div class="cols_table_cell cols_table_cell_button"><button id="remove-col-1" class="button_remove_col"><img src="remove_col-25.png"></button></div>
        </div>
    </div>
</div>

然后,使用下面的代码,我想用类cols_table_row拖动div并将其放入类cols_table_row的其他div:

var colIdSource;
$('.cols_table_row').on({
    dragstart: function (e) {
        colIdSource = e.target.id;
        e.originalEvent.dataTransfer.setData("colIdSource", colIdSource);
        e.originalEvent.dataTransfer.setData("rowIdSource", row.id);
    },
    dragenter: function (e) {},
    dragleave: function (e) {},
    dragover: function (e) {
        e.preventDefault();
    },
    drop: function (e) {
        var colIdTarget = $(this).attr("id");
        var colIdSource = e.originalEvent.dataTransfer.getData("colIdSource");
        if (colIdSource !== colIdTarget) {
            var rowIdSource = e.originalEvent.dataTransfer.getData("rowIdSource");
            var rowIdTarget = $(this).parent().attr("id");

            console.log("colIdSource = " + colIdSource);
            console.log("colIdTarget = " + colIdTarget);
            console.log("rowIdSource = " + rowIdSource);
            console.log("rowIdTarget = " + rowIdTarget);

        }
    },
    dragend: function (e) {},
    click: function (e) {}
});

当我使用id col_0拖动div并将其放入id为col_1的div时,这是日志输出。我没关系:

colIdSource = col_0
colIdTarget = col_1
rowIdSource = 0
rowIdTarget = cols_table_row_0_body

但是当我用id col_1拖动div并将其放入id为col_0的div时,输出乘以2:

colIdSource = col_1
colIdTarget = col_0
rowIdSource = 0
rowIdTarget = cols_table_row_0_body
colIdSource = col_1
colIdTarget = col_0
rowIdSource = 0
rowIdTarget = cols_table_row_0_body

为什么?

Here's the jsfiddle但是使用alert而不是console.log,并且它可以工作:在两种情况下都只显示4个警报,而不是在#2中使用console.log显示8个

1 个答案:

答案 0 :(得分:0)

我解决了离开jquery的问题,并制作了100%的javascript代码。 我认为问题来自构建ui然后调用脚本的函数。 谢谢大家。