我有一个包含多列的表。我有两个选择框:
假设我先单击#colorselect
选择框。我是否需要先使用#columnselect
选择框创建另一个功能,然后再#colorselect
创建内部功能?
这是我到目前为止的内容:
$(function(){
$("#colorselect").change(function() {
$("#colorselect option:selected").each(function() {
if($(this).attr("value")=="red") {
clr="red"
}
if($(this).attr("value")=="green") {
clr="green"
}
if($(this).attr("value")=="") {
clr="yellow"
}
};
$("#columnselect").change(function() {
$("#columnselect option:selected").each(function() {
if($(this).attr("value")=="column1") {
columnhighlightsel="Column1"
}
if($(this).attr("value")=="column2") {
columnhighlightsel="Column2"
}
if($(this).attr("value")=="") {
columnhighlightsel="Column2"
}
}
for (var i = 0; i<= $("#table th").length; i++) {
if($.trim($("#dailytable th:nth-child("+i+")").text()) === columnhighlightsel) {
varcolumnhighlight=i
}
};
$("#table tbody tr td:nth-child("+columnhighlight+")").each(function() {
$(this).css('background-color', clr);
};
我只想编写一个函数来处理任一列中的更改。执行代码之前,请检查其中一个是否为空。然后顺序就没关系了。
然后只需使颜色中的值选择所需的颜色,然后将该列作为该列的编号即可:
$('#colorselect, #columnselect').change(function() {
var $color = $('#colorselect').val(),
$column = $('#columnselect').val();
if ($color && $column) {
$("#table tbody tr td:nth-child("+$column+")").each(function(){
$(this).css('background-color', $color);
};
}
});
根据您的值,您可能必须将if条件调整为更具体。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句