简体   繁体   English

如何在显示时启动我的 td 并在单击时显示它们

[英]How to start my td's as revealed and unreveal them on click

Example of my table So basically im making a minesweeper game with PHP/Javascript.我的桌子的例子所以基本上我用 PHP/Javascript 制作了一个扫雷游戏。 The thing is that I would like my td's to be revealed at first, and when you click on a td it unreveals itself including the content.问题是我希望首先显示我的 td,当您单击 td 时,它会显示自己,包括内容。 Since im very new to programming I need all the help I can get.由于我对编程非常陌生,因此我需要所有可以获得的帮助。 And if possible, explain what does what with comments or so.如果可能的话,用评论来解释什么是什么。 posted below is my javascript that im using.下面发布的是我使用的javascript。 If you need the PHP code as well, let me know :) BUG = mine如果您还需要 PHP 代码,请告诉我 :) BUG = 我的

$(document).ready(function () {
    $("td").click(function () {
        if ($(this).text() == '') {
            $(this).css('background-color', 'DarkGray');
            console.log("Je hebt op een leeg vakje geklikt");
        }

        if ($(this).text() == 'BUG') {
            $("td:contains('BUG')").css('background-color', 'Gray');
            $("td:not(:contains('BUG'))").css('background-color', 'DarkGray');
            console.log("Je hebt op een bug geklikt");
        }

        $("#renew").click(function () {
            location.reload();
        });
    })
});

You want to hide / reveal specific table cell texts based on user clicks.您想根据用户点击隐藏/显示特定的表格单元格文本。

You cannot do that with background-color, since that will not override the text (the text will still be written above the background color).你不能用 background-color 做到这一点,因为它不会覆盖文本(文本仍然会写在背景颜色之上)。

Trying not to diverge from your current implementation, you could make all your td cells have font-size: 0px, and then when the user clicks, change the font size to be mlarger than 0 so the text shows (along with changing backgrounds and whatever else you want).尽量不偏离您当前的实现,您可以使所有 td 单元格都具有 font-size: 0px,然后当用户单击时,将字体大小更改为大于 0,以便文本显示(以及更改背景和其他任何内容)其他你想要)。

CSS CSS

td{
font-size: 0px;
}

JS JS

$(document).ready(function () {
$("td").click(function () {

    $(this).css('font-size', '18px');

    if ($(this).text() == '') {
        $(this).css('background-color', 'DarkGray');
        console.log("Je hebt op een leeg vakje geklikt");
    }

    if ($(this).text() == 'BUG') {
        $("td:contains('BUG')").css('background-color', 'Gray');
        $("td:not(:contains('BUG'))").css('background-color', 'DarkGray');
        console.log("Je hebt op een bug geklikt");
    }

    $("#renew").click(function () {
        location.reload();
    });
})

}); });

Edit: This is a clunky way of implementing the desired functionality.编辑:这是实现所需功能的笨拙方式。 A better way would be to, instead of adding BUG text directly into your td, write your td to hold information about whether or not it contains a BUG.更好的方法是,不要将 BUG 文本直接添加到您的 td 中,而是编写您的 td 以保存有关它是否包含 BUG 的信息。

For example:例如:

<td data-type="BUG"></td>
<td data-type="NOT"></td>
<td data-type="BUG"></td>

Then, when the user clicks on a td, you check if data-type is BUG, and if it is, add 'BUG' into the clicked td, and in that way you 'reveal' what the td was.然后,当用户单击 td 时,您检查数据类型是否为 BUG,如果是,则将“BUG”添加到单击的 td 中,这样您就可以“揭示”td 是什么。

For example with before mentioned html:例如前面提到的html:

$("td").click(function(){
   if($(this).attr("data-type") === "BUG"){
       //user clicked on a td with a BUG
       $(this).text('BUG');
   }
});

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

相关问题 为什么单击按钮时我的隐藏表没有显示? - Why is my hidden table not being revealed on button click? 如何对齐我的TD与相等的宽度? - how to align my td's with equal width? 如何在TD中添加没有文本的类? - How to add a class to TD's without text in them? 如何使用jQuery更改我的两个td的背景颜色? - how to change background color of my two td's using jQuery? 由迭代创建的表格单元格是隐藏的 - 如何使用按钮取消显示每个条目? - Table cells, created by iteration, are hidden - how to unreveal each entry with a button? 如何使点击可编辑 - How to make <td> on click to be editable 单击网格中的单元格时,如何获取单元格的列标题和行的第一个td? - How to get a cell's column header and row's first td when I click a cell in grid? 如何通过单击Javascript来使HTML TD设置为overflow:hiddenscroll隐藏滚动? - How can I make an HTML TD that's set to overflow:hidden scroll smoothly with the click of a button through Javascript? 如何使用 JavaScript/jQuery 在点击时更改特定 TD 的背景颜色? - How do I change a specific TD's background color on click with JavaScript/jQuery? 您如何在表格的 tr 上仅显示特定 td 文本的第一行并在单击时展开? - How do you show just the first line of the particular td's text on a tr of a table and expand on click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM