【问题标题】:Connect single image to single cell in a table with Javascript使用Javascript将单个图像连接到表格中的单个单元格
【发布时间】:2017-01-07 10:07:45
【问题描述】:

我对编程和这个社区都很陌生,我会尽量说清楚。我需要创建一个 16x16 的 HTML 表格,每个单元格中都有一个按钮,每个单元格显示不同的图像 - 笑脸 - 每个单元格,而不会为不同的单元格显示相同的笑脸。我有点卡住了,我也很感激一个提示,只是尝试自己继续前进。谢谢!这是我创建表格并显示图像的代码:

function displayImage(){
    var num = Math.floor(Math.random() * 256);
    document.canvas.src = '../../output/'+ imagesArray[num]+ '.png';
    document.getElementById("YourImage").style.visibility = "visible";
}
for(var r = 0; r < rows;r++)
{
    table += '<tr>';
    for(var c= 0; c< cols;c++)
    {
        table += '<td style="width:50px" id="(i++)">' + '<button type="button" onclick="displayImage()">' +
        "show me" + '</button>' + '</td>';
     }
     table += '</tr>';
}
document.write('<table id="grid" border="1">' + table + '</table>');

(为便于阅读,未显示 ImagesArray,但它只是收集了所有 256 个二进制组合)。 这样做的问题是,每次我单击一个按钮时,它都会显示一个随机图像,如果我再次单击同一个按钮,它会显示另一个,而我希望它总是为同一个按钮显示相同的图像。 注意:我不能在之后删除按钮或类似的技巧,因为玩家(这是一个 oTree 实验)需要能够随时回到他看到的笑脸。 非常感谢您的帮助!

【问题讨论】:

  • 您应该为图像编号(每行,col)创建一个随机数组并将它们存储在一个数组中。然后当您想在 button_click 上显示图像时使用该信息
  • 感谢@S.Serp 的回复,但我很难理解(很可能是我的错,因为我使用 HTML 和 JS 还不到一个月)。您会建议使用 row,col 坐标创建一个数组,并使用它来唯一地将二进制代码(在我的情况下为 00110011、01010101、...)连接到每一行、col 坐标?但是怎么做呢?
  • 我已经发布了答案,看看并说出您的评论或任何问题

标签: javascript html html-table otree


【解决方案1】:

我会更改生成随机数的点,并将其从显示函数推进到表的初始化。

创建一个包含从 0 到 255 的数字的数组。在遍历行和列以创建表格时,从该数组中选择一个随机数并将其分配给按钮的 id 属性。一定要从数组中删除这个数字,这样它就不能再用于下面的按钮了。这样,将使用 256 个数字中的每一个,但顺序是随机的。当用户点击一个按钮时,使用displayImage(this.id)按钮的id作为参数调用displayImage()。因此,总是会使用相同编号的按钮来获取相应的图像。

代码:

// call displayImage() with parameter
function displayImage(num){
    document.canvas.src = '../../output/'+ imagesArray[num]+ '.png';
    document.getElementById("YourImage").style.visibility = "visible";
}

// create array 'nums' with numbers from 0 to 255 (= rows * cols)
var nums = [];
for (var i = 0; i < (rows * cols); i++) {
    nums.push(i);
};

for(var r = 0; r < rows; r++)
{
    table += '<tr>';
    for(var c = 0; c < cols; c++)
    {
        // get random index in array 'nums'
        var random = Math.floor(Math.random() * nums.length);
        // assign an id to the button with a random number from 'nums' and use this id in displayImage()
        table += '<td style="width:50px" id="(i++)">' + '<button id="' + nums[random] + '" type="button" onclick="displayImage(this.id)">' +
        "show me" + '</button>' + '</td>';
        // remove the already used random number from 'nums'
        nums.splice(random, 1);
     }
     table += '</tr>';
}
document.write('<table id="grid" border="1">' + table + '</table>');

这样您将有相同的图像连接到每个按钮。它在创建表格时是随机的,因此每次加载页面时顺序都不同。

我假设您使用的是 HTML5,因为只有这样才允许 id 属性仅包含数字。

注意:在创建&lt;td&gt; 元素时,我不知道您究竟想用id="(i++)" 实现什么。你这样做的方式只是用字符串(i++) 命名每个 id,这与 id 应该是唯一的想法相矛盾。如果您想命名从 td-0td-255 升序的 id,您应该将行更改为:

table += '<td style="width:50px" id="td-' + (r * rows + c) + '">' + '<button id="' + nums[random] + '" type="button" onclick="displayImage(this.id)">' + "show me" + '</button>' + '</td>';

你不能只使用数字而不添加类似td-的东西,因为按钮已经有纯数字作为id。

【讨论】:

  • 在您的实现中,您可能有两个或更多具有相同图像编号的按钮!这不是完全独一无二的!
  • @S.Serp 真的吗?在循环中用于按钮的数字会立即用nums.splice() 删除,因此它们不能被使用两次(如果我没有遗漏任何东西)。
  • 哦,是的,我没注意到
  • 很好的解决方案!我的问题基本上是按钮的 id,我错过了“this.id”部分并弄乱了标识(我在某处看到了 i++,但我不太相信它会起作用。非常感谢大家,我希望我在某个时候也能起到回报的作用;)
【解决方案2】:

试试这个:

var rows = 16, cols = 16;
var table = '';

function shuffle(array) { // shuffle (randomize) array elements
  var i = array.length, j, x;
  while (i) {
    j = Math.random() * i-- | 0;
    x = array[i];
    array[i] = array[j];
    array[j] = x;
  }
}

var rNums = new Array(rows * cols);
for (var i = 0; i < rows * cols; i++) {
  rNums[i] = i;
}
shuffle(rNums);

function displayImage(num) {
  //var num = Math.floor(Math.random() * 256);
  document.canvas.src = '../../output/'+ imagesArray[num]+ '.png';
  //document.getElementById("canvas").innerHTML = num;
  document.getElementById("YourImage").style.visibility = "visible";
}

var i = 0;

for (var r = 0; r < rows; r++) {
  table += '<tr>';
  for (var c = 0; c < cols; c++) {
    table += '<td style="width:50px" id="' + i + '"><button type="button" onclick="displayImage(' + rNums[i] + ')">show me</button></td>';
    i++; //go to next element of rNums array
  }
  table += '</tr>';
}
document.write('<table id="grid" border="1">' + table + '</table>');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 2021-09-22
    • 1970-01-01
    • 2011-12-17
    • 2021-02-20
    • 1970-01-01
    • 2013-06-03
    相关资源
    最近更新 更多