简体   繁体   English

将画布元素与另一个表单元格值交换

[英]Swap canvas element with another table cells value

I have a 3x3 HTML table with each element storing a separate <canvas> element 我有一个3x3 HTML表,每个元素存储一个单独的<canvas>元素
There is one cell in the table containing text instead of a <canvas> 表中有一个单元格包含text而不是<canvas>
When a cell containing an image is clicked and the cell to its right contains text, I'm trying to swap that cell's canvas with the text stored in the neighbouring cell. 当单击包含图像的单元格并且其右侧的单元格包含文本时,我正在尝试将该单元格的画布与存储在相邻单元格中的文本交换。
With the following code every time I click a cell, the canvas element disappears and only swaps the text, leaving the cell on the right no longer containing text or a <canvas> 每次单击一个单元格时,使用以下代码,canvas元素消失,仅交换文本,而右侧的单元格不再包含文本或<canvas>
I tried accessing the canvas using ("#blankCell").html() but it does not work .. anybody know how to access this content for swapping of values? 我尝试使用("#blankCell").html()访问画布,但是它不起作用..有人知道如何访问此内容以交换值吗?

I created a simplified jsFiddle for this: http://jsfiddle.net/bobbyrne01/dbMnM/1/ 我为此创建了一个简化的jsFiddle: http : //jsfiddle.net/bobbyrne01/dbMnM/1/

$(function () {

    var $tbl = $('<table border="1">').attr('id', 'grid');
    var $tbody = $('<tbody>').attr('id', 'tableBody');
    var tileCount = 0;
            var rowCount = $("#numOfPieces").val();

    for (var i = 0; i < rowCount; i++) {

        var trow = $("<tr>").attr('id', 'row' + i); // New row

        for (var j = 0; j < rowCount; j++) {

            $cell.append(canvasArray[tileCount]); // Each data cell contains separate canvas element
            $cell.appendTo(trow); 
            tileCount++;
        }

        trow.appendTo($tbody);
    }

    $tbl.append($tbody);
    $('table').remove(); // removes previous table if it existed
    $('body').append($tbl);
});

$('#grid tr:nth-child(2) td:last').prev().text("empty");
$('#grid tr:nth-child(2) td:last').prev().attr('id', 'blankCell');

Listens for clicks .. 监听点击..

   $('body').on('click', '#grid td', function(e) {

    var $this = $(this);
    if ($(this).closest('td').next("#blankCell").length){

        blank = $(this).closest('td').next("#blankCell");

        if (blank.length) {
            //alert('blank to the right');
            $this.before(blank);
        }
        $(this).attr('id', 'blankCell');
        $(this).closest('td').next("#blankCell").attr('id', 'piece');

    }

Appreciate any help! 感谢任何帮助!

Instead of using canvas elements for swapping, it now only has text. 现在不再使用画布元素进行交换,而是仅包含文本。
The goal is if a user selects a cell beside the blankCell I would like to swap that cell with the blankCell , if the user selects a cell not beside blankCell I would like nothing to happen. 我们的目标是,如果一个用户选择了旁边的一个小区blankCell我想调换该小区,其blankCell ,如果用户选择了一个小区旁边不blankCell我想没有什么事情发生。
Only concerned with left and right detection for now. 现在仅涉及左右检测。

Make it simple and just exchange the whole table cells, not their contents or content strings and attributes: 简单点,只交换整个表单元格,而不交换它们的内容或内容字符串和属性:

$('#grid').on('click', 'td', function(e) {
    var $this = $(this),
        blank = $this.next("#blankCell");
    if (blank.length) {
        //alert('blank to the right');
        $this.before(blank);
    }
});

OK, some more complicated code for checking the position included: 好的,包括一些更复杂的代码来检查位置:

var empty = $("#empty").get(0);
$('#grid').on('click', 'td', function(e) {
    if (!empty || this == empty) return; // abort, abort!

    var currow = this.parentNode,
        emptyrow = empty.parentNode;
    var cx = this.cellIndex,
        cy = currow.rowIndex,
        ex = empty.cellIndex,
        ey = emptyrow.rowIndex;
    if (cx==ex && Math.abs(cy-ey)==1 || cy==ey && Math.abs(cx-ex)==1) {
        // empty and this are next to each other in the grid
        var afterempty = empty.nextSibling,
            afterthis = this.nextSibling;
        currow.insertBefore(empty, afterthis);
        emptyrow.insertBefore(this, afterempty);
    }
});

Demo at jsfiddle.net jsfiddle.net上的演示

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

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