How can I display letters using html table cells as colored pixels?

后端 未结 2 1025
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-27 11:49

Let\'s say I have an html table (50 x 50 cells) with a yellow background. I would like to use the background colors of the cells to print letters (A-Z) (e.g. some cells blue and

2条回答
  •  甜味超标
    2021-01-27 12:07

    Create the cells with a loop such as:

    for(var i=65;i<=90;i++) {
        console.log(String.fromCharCode(i));
     }
    

    (65 is the char code for A, 90 for Z). in the loop, attach id with the char. something like "charA", "charB". Than you can access the fields searching the id <"char" + letter>.

    if you know only the (x,y) in the 50x50 grid, calculate which letter it is with:

    String.fromCharCode(65+y*50+x);
    

    Or take the value of it from the elment itself

    $(this).val();
    

    Or from attribute you attach to it:

    $(this).attr('letter')
    

提交回复
热议问题