Javascript 更改特定表格的所有颜色单击
iam使用以下代码切换td颜色Javascript 更改特定表格的所有颜色单击,javascript,jquery,html,css,Javascript,Jquery,Html,Css,iam使用以下代码切换td颜色 <html> <head> <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script> $(function(){ $("td").click(function()
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(function(){
$("td").click(function(){
$(this).addClass("on").parent().siblings("tr").find("td").removeClass("on");
});
});
</script>
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
.on { background-color:red; color:#ffffff; }
</style>
</head>
<body>
<table class="mytable" border=1>
<tbody>
<tr>
<td>Hello World</td>
<td>Hello World1</td>
<td>Hello World2</td>
</tr>
<tr>
<td>Hello World</td>
<td>Hello World1</td>
<td>Hello World2</td>
</tr>
<tr>
<td>Hello World</td>
<td>Hello World1</td>
<td>Hello World2</td>
</tr>
</tbody>
</table>
</body>
</html>
以上代码通过切换td颜色可以正常工作,请检查
但现在我需要做三件事
上面的代码适用于所有tds,我需要它只适用于表类mytable的最后一列,
我需要添加一个按钮,当点击该按钮时,应该将表类mytable的所有td颜色更改为白色
选择部分细胞时,完整行应为红色。
请帮我修一下
只需将jQuery选择器更改为td:last child 对于第二个问题,您需要HTML和jQuery: HTML:
<button id="tableButton">White</button>
这是一种方法,您当然可以使用最后一个子选择器再次选择td,但这段代码是一个很好的起点:
HTML
//将以下代码添加到html中
<input type="submit" value="submit" id="button">
HTML
据评论
关于你的第二个问题
$("#Button_for_color").click( function() {
$("table .mytable").css({"background":"black", "color":"white"});
});
第一次我不确定,但它的工作原理是
$(function(){
$("td:last-child").click(function(){
//You can play here
});
希望第二个问题也适用于第三个问题:
$(function(){
$("td").click(function(){
$("td").removeClass("on");
$(this).addClass("on").parent().siblings("tr").find("td").removeClass("on");
});
});
现在检查这个
$(function(){
$("tr:last-child td").click(function(){
$("tr:last-child td").addClass("on");
});
});
感谢您更新此答案,这是我需要的..好吧,现在选择特定单元格时,特定单元格变为红色,当选择特定单元格时,是否有方法可以更改完整行的颜色??请帮我做这个……你好,密码悖论,它不起作用了。。正如我提到的。。。用户应该只能选择最后一列单元格如果他们点击任何其他单元格颜色不应该改变,但当选择特定单元格时,完成的行应该变成红色,而不仅仅是一个单元格现在正在发生请检查我更新的问题亲爱的。。。我还增加了第三点。。有办法解决吗?从来没有。。我不投任何人的反对票……好吧,检查我更新的问题“Abody”,我需要修正第三点,现在有人这样做了。我不明白为什么。我为你的第三个问题更新了我的答案…请检查它。如果它有用并且对你有用,请接受它。不,它不起作用。。。正如我所指定的,用户应该只能选择最后一列单元格,但当选择特定单元格时,Complete行应该变为红色,而不仅仅是现在发生的一个单元格…只需检查一次,因为它在JSFIDLE中对我有效。对于第三个oneno..在您的代码中,它只选择最后一列单元格,当选中时,它只高亮显示选中的单元格……但正如我所提到的,它应该高亮显示整行,但只有最后一列单元格才可以单击。。除了最后一列单元格外,表中的其他单元格不应可单击
$(function() {
$(".mytable tr td:last-child").click(function() {
$(this).addClass("on").parent().siblings("tr").find("td.on").removeClass("on");
})
$('#change-color').click(function() {
$('.mytable td.on').removeClass('on');
});
});
$(function() {
$(".mytable tr td:last-child").click(function() {
$('td.on').removeClass('on');
$(this).parent().find('td').addClass("on");
})
$('#change-color').click(function() {
$('.mytable td.on').removeClass('on');
});
});
$("#Button_for_color").click( function() {
$("table .mytable").css({"background":"black", "color":"white"});
});
$(function(){
$("td:last-child").click(function(){
//You can play here
});
$(function(){
$("td").click(function(){
$("td").removeClass("on");
$(this).addClass("on").parent().siblings("tr").find("td").removeClass("on");
});
});
$(function(){
$("tr:last-child td").click(function(){
$("tr:last-child td").addClass("on");
});
});