hot仅从当前拖动td开始选择

时间:2012-10-22 13:35:56

标签: jquery

  $(document).live('mouseup', function () { flag = false; });
            $(".csstablelisttd").live('mousedown', function (e)
            {

                $(".csstdselected").removeClass("csstdselected").addClass('csstdgreen');
                //This line gets the index of the first clicked row.
                lastRow = $(this).closest("tr")[0].rowIndex;
                var rowIndex = $(this).closest("tr").index();
                var colIndex = $(e.target).closest('td').index();
                $(".csstdhighlight").removeClass("csstdhighlight");


                //$('td:nth-child(' + colIndex + ')').addClass('csstdhighlight');

                //$(this).children(":gt(" + colIndex + ")").addClass("csstdhighlight");
                $(this).children(":not(:first)").addClass("csstdhighlight");
                e.preventDefault();
                flag = true;
                return false;
            });
            document.onmousemove = function () { return false; };
            $(".csstablelisttd").live('mouseenter', function (e)
            {
                // Compares with the last and next row index.
                currentRow = $(this).closest("tr")[0].rowIndex;
                if (lastRow == currentRow || lastRow == currentRow - 1 || lastRow == currentRow + 1)
                {
                    lastRow = $(this).closest("tr")[0].rowIndex;
                } else
                    return;

                if (flag)
                {


$(this).children(":not(:first)").addClass("csstdhighlight");
                        e.preventDefault();
                        if ($(".csstdhighlight").length >= 6)
                        {
                            //alert("Time slot not more than 45 minutes.");
                            flag = false;
                        }
                    }
                });

在我的表中看到我要做的有8列和48行用户可以开始只拖动一列但超过1行

Demo

1 个答案:

答案 0 :(得分:0)

使用nth-child选择器:

$('td:nth-child(' + (colIndex + 1) + ')').addClass('foo');

Here是对你的jsFiddle的修改。