使用jQuery的addClass是有效的

addClass with jQuery Isn't Working

本文关键字:有效 addClass 使用 jQuery      更新时间:2023-09-26

我正在改变表中2行的数据位置。向上箭头和向下箭头图像的类设置不正确。

如果position为1,那么它应该只是一个向下的箭头。同样,如果position是最后一个,那么应该只有向上箭头可用。在其他情况下,两个箭头都应该是可见的。

这是我尝试的JavaScript:

function sortTable( tablename )
{
    var count = $("#" + tablename + " tr").length;
    $("#" + tablename + " tr").each(function()
    {
        var that = $(this);
        if (that.data("position") == 1)
        {
            $(this)
                .find("img.rowUp")
                .addClass("imgDisplayNone" );
        }
        else if (that.data("position") == count)
        {
            $(this)
                .find("img.rowDown")
                .addClass("imgDisplayNone" );
            console.log("positon == anzahl" + that.data("position") + " anzahl " + count);
        }
        else if(that.data("position")>1 && that.data("position")< count)
        {
            $(this)
                .find("img.rowUp")
                .removeClass("imgDisplayNone" );
        }
    });
}

我认为问题是源代码和dom操作之间的差异,但我不确定如何修复它。

我读到我需要jQuery的.index,但我不知道如何实现它。

谢谢你的帮助。

你可以直接使用CSS:

tr:first-child img.rowUp { display: none }
tr:last-child img.rowDown { display: none }
http://jsfiddle.net/Q6Q8T/