【问题标题】:pass images on a board在板上传递图像
【发布时间】:2021-03-21 07:59:56
【问题描述】:

你好,我已经在图像中创建了这个数据库。在 b_color(我有黑色或红色)。在 piece_color(红色和黑色)。在piece上,我有pawn1 (P1) 和pawn2(P2)。 所有这些都在mysql上。 在我的索引页面上,我创建了 2 个原始数据和 12 列,因此它将是 24 个框。我已经使用此代码创建了它。

$(function () {
    draw_empty_board();
    fill_board();
    $('#do_move').click(do_move);
    $('#chess_reset').click(do_reset);
});


function draw_empty_board() {
    var t='<table id="chess_table">';
    for(var i=2;i>0;i--) {
        
        t += '<tr>';
        t += '<td class="line_label">'+i+'</td>';
        for(var j=1;j<13;j++) {
            
        
            t += '<td class="chess_square" id="square_'+j+'_'+i+'">' + j +','+i+'</td>'; 
            
        }
        t+='</tr>';
    }
    t += '<tr><td class="column_label line_label"></td>';
    for(var j=1;j<13;j++) {
        
        t += '<td class="column_label">'+j+'</td>';
    }
    t+='</tr>';
    t+='</table>';
    
    $('#chess_board').html(t);
}

function fill_board() {
    $.ajax({url: "chess.php/board/", success: fill_board_by_data });
    
}

function fill_board_by_data(data) {
    for(var i=0;i<data.length;i++) {
        var o = data[i];
        var id = '#square_'+ o.x +'_' + o.y;
        var c = (o.piece!=null)?o.piece_color + o.piece:'';
        var im = (o.piece!=null)?'<img class="piece" src="images/'+c+'.png">':'';
        var im = c;
        $(id).addClass(o.b_color+'_square').html(im);
        
    }

代码正在运行。我想用两个图像填充这个板。 我创建了一个名为 images 的文件夹,所以我将拍摄照片并用这两个图像填充我的板。 https://p7.hiclipart.com/preview/143/606/545/clip-art-bullet-thumbnail.jpg 黑色 https://www.clker.com/cliparts/d/5/T/A/K/2/red-circle-hi.png 为红色。 我已将这些照片保存在 png 中,并且在 images 文件夹中。 问题是我认为我的数据库..

【问题讨论】:

  • 这就像我理解你的问题......但最后你说你认为问题出在你的数据库上!您的意思是,您发现使用 Ajax 从数据库中投射这些棋子有困难吗?请澄清这里缺少什么..!您介意分享您的数据的示例代码吗?
  • 我已经使用 jQuery 解决了您的问题...我明天会回来,我希望有一个静态数据样本和更好的解释这里缺少什么? :)
  • 是的,我太累了,我以为我的数据库有问题...我说的那句话“问题是我认为我的数据库..”,算了。
  • @Bilel 我想在 [1,1] 中有黑色的,黑色的 16 个图像。在位置 [12,2] 我想要红色的 16 个
  • 你又把数字 16 弄糊涂了! :) 好的,你能解释一下你的代码的目标吗?或发送样本静态数据?同时,您使用控制台检查 ajax 调用的响应......这可能是您的问题。您也可能遇到 contentType 问题。

标签: javascript jquery mysql image


【解决方案1】:

我希望这段代码对你有所帮助...我还建议使用 Css(点)作为图片的替代品。

  //Because I can't rename hosted images filenames
  //I'm using this color dictionary
  let colors={black:"https://i.imgur.com/dL3J8XS.jpeg",red:'https://i.imgur.com/ZEZLSOo.png'}
  //
  
  let items = [
  {
    "x": 1,
    "y": 1,
    "piece":"p",
    "piece_color": "black",
    "b_color": "black"
  },
  {
    "x": 12,
    "y": 2,
    "piece":"p",
    "piece_color": "red",
    "b_color": "red"
  }
  ];
  $(function () {
    draw_empty_board();
    fill_board();
    //$('#do_move').click(do_move);
    //$('#chess_reset').click(do_reset);
});

  function draw_empty_board() {
    var t='<table id="chess_table">';
    for(var i=2;i>0;i--) {
        
        t += '<tr>';
        t += '<td class="line_label">'+i+'</td>';
        for(var j=1;j<13;j++) {
            
        
            t += '<td class="chess_square" id="square_'+j+'_'+i+'"></td>'; 
            
        }
        t+='</tr>';
    }
    t += '<tr><td class="column_label line_label"></td>';
    for(var j=1;j<13;j++) {
        
        t += '<td class="column_label">'+j+'</td>';
    }
    t+='</tr>';
    t+='</table>';
    
    $('#chess_board').html(t);
}
function fill_board() {
    //$.ajax({url: "chess.php/board/", success: fill_board_by_data });
    //I'm a calling static data here
    fill_board_by_data(items);
    
}

function fill_board_by_data(data) {
    
    for(var i=0;i<data.length;i++) {

        var o = data[i];
        var id = '#square_'+ o.x +'_' + o.y;
        //var c = (o.piece!=null)?o.piece_color + o.piece:'';
        var c = (o.piece!=null)?o.piece_color:'';
        
        //$(id).append('<span class="'+c+'dot"></span>');
        var im = (o.piece!=null)?'<img class="piece" src="'+colors[c]+'.png">':'';
        $(id).addClass(o.b_color+'_square').html(im);
        
    }
}
  table{
    border-collapse:collapse;
    border:2px solid;
  }
  td{
    width:25px;
    height:25px;
    border:1px solid;
  }
  
  /*this is a suggested Css alternative to images */
.reddot {
  height: 25px;
  width: 25px;
  background-color: red;
  border-radius: 50%;
  display: inline-block;
}
    .blackdot {
  height: 25px;
  width: 25px;
  background-color: #000;
  border-radius: 50%;
  display: inline-block;
}
img{width:25px;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="chess_board"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 2011-12-12
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 2022-10-24
    相关资源
    最近更新 更多