简体   繁体   English

按下按钮即可更改表格单元格的颜色

[英]Change table cell color by pressing button

I Would like to change the color of the next cell in the table by pressing a button. 我想通过按下按钮来更改表格中下一个单元格的颜色。

For example: If the left button is pressed the color of the next cell in the left will change from white to red and the actual cells color will change from red to white and so on. 例如:如果按下左按钮,则左下一个单元格的颜色将从白色变为红色,而实际单元格的颜色将从红色变为白色,依此类推。

In this way we have the illusion that the red square i moving by pressing a button. 通过这种方式,我们有一种幻想,就是我按下按钮会移动红色方块。

Is it possible to do this with Jquery and CSS? 是否可以使用Jquery和CSS做到这一点?

在此处输入图片说明

As a full example: 举一个完整的例子:

HTML: HTML:

<table>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
        <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
        <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
        <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
</table>

<button id="left">Left</button>
<button id="right">Right</button>
<button id="up">Up</button>
<button id="down">Down</button>

JavaScript / jQuery: JavaScript / jQuery:

var position = 0;

$("#left").click(function() {
   position = position -1;
    setCurrentPosition();
});

$("#right").click(function() {
   position = position + 1;    
   setCurrentPosition();
});

$("#up").click(function() {
   position = position -4;
    setCurrentPosition();
});

$("#down").click(function() {
   position = position + 4;    
   setCurrentPosition();
});

function setCurrentPosition() {
    $(".selected").removeClass();
    $("table td").each(function(value) {
        if (value == position) {
            $(this).addClass("selected");
        }
    });
}

CSS: CSS:

table td {
    border: 1px solid black;
    padding: 30px;
}

.selected {
    background-color: red;    
}

http://jsfiddle.net/nzRxu/ http://jsfiddle.net/nzRxu/

Sure you can here is the whole example now how i think this should be done 当然您可以在这里找到整个示例,我认为应该怎么做

http://jsfiddle.net/puEbp/2/ http://jsfiddle.net/puEbp/2/

HTML HTML

<table>
<tr>
    <td class="red"></td>
    <td></td>
    <td></td>
    <td></td>
</tr>
<tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
</tr>
<tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
</tr>
</table>
<button id="leftButton">left</button>
<button id="rightButton">right</button>
<button id="downButton">down</button>
<button id="upButton">up</button>

JavaScript JavaScript的

$(function(){
$('#rightButton').on('click', function(e){
    e.preventDefault();
    var actualTD = $('td.red');
    if (actualTD.next('td').length > 0) {
        actualTD.next('td').addClass('red');
        actualTD.removeClass('red');
    }        
});
$('#leftButton').on('click', function(e){
    e.preventDefault();
    var actualTD = $('td.red');
    if (actualTD.prev('td').length > 0) {
        actualTD.prev('td').addClass('red');
        actualTD.removeClass('red');
    }        
});    
$('#downButton').on('click', function(e){
    e.preventDefault();
    var actualTD = $('td.red'),
        actualParentTD = actualTD.parent('tr'),
        index = actualTD.index();

    if (actualParentTD.next('tr').length > 0) {
        actualParentTD.next('tr').find('td:eq('+index+')').addClass('red');
        actualTD.removeClass('red');
    }        
});
$('#upButton').on('click', function(e){
    e.preventDefault();
    var actualTD = $('td.red'),
        actualParentTD = actualTD.parent('tr'),
        index = actualTD.index();

    if (actualParentTD.prev('tr').length > 0) {
        actualParentTD.prev('tr').find('td:eq('+index+')').addClass('red');
        actualTD.removeClass('red');
    }        
});
});

CSS CSS

table td{
border: 2px solid gray;
width: 50px;
height: 50px;
}
.red{
background: red;
}

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

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