2

我相信可以将一个 DOM 对象数组传递给 jQuery 的选择器,这样您就可以同时操作多个对象。我已经尝试按如下方式执行此操作,但由于某种原因无法使其正常工作......

$(Sel).animate({
        backgroundColor: "#FF0000"
    }, 250, 'linear', function() {

        $(this).animate({
            backgroundColor: "#FFFFFF"
        }, 250, 'linear');

    });

真的有可能做到这一点,还是我在吠叫错误的树?

我已经把这个 jsFiddle放在一起来测试一下。目的是制作一个预订系统,其中选择半小时的时段,因此我需要操纵“这个”和下一行下面的单元格。

非常感谢任何建议。

小提琴的代码:

function HighlightCells() {

    $('table#Calendar tbody tr td:not(".TimeCell")').live('mouseenter', function() {
        var Sel = new Array();
        Sel[1] = $(this);

        // Count number of previous TDs. Resut is base 0
        var NumIn = $(this).prevAll('td').length;

        // Increment count to compensate for nth-child being base 1
        NumIn++;

        var NextRow = $(this).closest('tr').next('tr');

        Sel[2] = $(NextRow).children("td:nth-child(" + NumIn + ")");

        // Animate the cell background colour red to white
        $(Sel).animate({
            backgroundColor: "#FF0000"
        }, 250, 'linear', function() {

            $(this).animate({
                backgroundColor: "#FFFFFF"
            }, 250, 'linear');

        });


        $('table#Calendar tbody td').live('mouseleave', function() {
            $(this).text("");
        });

    });

}

HighlightCells();
4

3 回答 3

3

您正在从一组 jQuery 对象中创建一个 jQuery 对象。你不能那样做,这是行不通的。

您需要创建Sel一个 DOM 元素数组(注意:数组是零索引的,Sel[1]实际上第二个元素也是如此,但是在构建数组时,.push除非您确实需要使用实际的键,否则请使用):

var Sel = [];  // this is preferred over `new Array()`
Sel.push($(this).get(0)); // or Sel.push(this)
// ...
Sel.push($(NextRow).children("td:nth-child(" + NumIn + ")").get(0));

或者先创建Sel一个 jQuery 对象,然后向其中添加元素。

var Sel = $();
Sel = Sel.add(this);
// ...
Sel = Sel.add($(NextRow).children("td:nth-child(" + NumIn + ")"));
// ...
Sel.animate({ // sel is already a jQuery object, so we don't need `$(Sel)`
于 2012-08-13T15:49:25.877 回答
2

您正在使用一组 jQuery 对象。相反,您需要一个 DOM 对象数组。

var Sel = new Array();
        Sel[1] = this;

Sel[2] = $(NextRow).children("td:nth-child(" + NumIn + ")").get();

但是,不应该是Sel[0] = thisandSel[1] = ...吗?

于 2012-08-13T15:41:19.093 回答
1

你可以这样做

var Sel = new Array();
Sel[1] = this;

Sel[2] = NextRow.children("td:nth-child(" + NumIn + ")")[0]; 
//  retrieves the DOM element  
// Also no need to wrap NextRow with $() since it's already a jQuery object

http://jsfiddle.net/wirey00/AX3C8/27/

于 2012-08-13T15:49:53.433 回答