简体   繁体   English

如何使用鼠标单击以颜色填充html表制成的网格?

[英]How to fill html table made grid with colors using mouse click?

I want to create a simple tool so that user can fill colors inside html table fields or whatever the alternative you can suggest. 我想创建一个简单的工具,以便用户可以在html表格字段中填充颜色,或者您可以建议其他替代方法。 Take a look at this page: http://www.olmares.com/Price%20and%20Availability.htm 看一下此页面: http : //www.olmares.com/Price%20and%20Availability.htm

I am looking for some tool, interface, Javascript or any other way so that my client fill in the table boxes with colors easily. 我正在寻找某种工具,界面,Javascript或任何其他方式,以便我的客户轻松在表格框中填充颜色。 How can I achieve this? 我该如何实现?

you can set csscalass for your table td by javascript. 您可以使用javascript为表td设置csscalass。

    <Table><tr>
<td cssclass="a"></td><td cssclass="b"></td>
</tr></table>

and your css: 和你的CSS:

.a{backgroundcolor:red}
.b{backgroundcolor:green}

you need to set up a click handler for your table cells that react by changing the color of the cell clicked in .. in jQuery it'd be something like 您需要为您的表格单元格设置一个点击处理程序,该处理程序通过更改在jQuery中单击的单元格的颜色来做出反应。

var current_color = 'red';

$('td').click(function() {
   $(this).css('background-color', current_color);
});

thus whatever the current_color is (changed perhaps via other user interaction) gets applied to subsequently clicked cells. 因此,无论current_color(可能通过其他用户交互更改)如何,都将其应用于随后单击的单元格。

I have added an extra, so when you click on highlighted td, it will remove it. 我添加了一个额外的内容,因此当您单击突出显示的td时,它将被删除。

Working link : http://jsfiddle.net/dEy2H/ 工作链接: http : //jsfiddle.net/dEy2H/

CSS 的CSS

.HighLight {background-color:#ff0000 !important;}

jQuery jQuery的

$(function() {
    $('td').click(function() {
        if ($(this).hasClass('HighLight'))
            $(this).removeClass('HighLight');
        else
            $(this).addClass('HighLight');
    });
});

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

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