简体   繁体   English

jQuery的addClass无法正常工作

[英]addClass with jQuery Isn't Working

I am changing the data-position of 2 rows in a table. 我正在更改表中2行的数据位置。 The classes for the arrow-up and arrow-down images are not being set correctly. 向上和向下箭头图像的类别设置不正确。

If position is 1, then it should be only an arrow-down. 如果position为1,则应该只是向下箭头。 Likewise, if position is the last, then only the arrow-up should be available. 同样,如果位置是最后一个,则仅应使用向上箭头。 In any other case, both arrows should be visible. 在任何其他情况下,两个箭头都应该可见。

Here is the JavaScript I tried: 这是我尝试过的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" );
        }
    });

}

I think the problem is the difference between the sourcecode and the DOM-manipulation, but I'm not sure how to fix it. 我认为问题是源代码和DOM操作之间的区别,但是我不确定如何解决它。

I have read that I need jQuery's .index , but I don't know how to implement it. 我已经读到我需要jQuery的.index ,但是我不知道如何实现它。

Thank you for your help. 谢谢您的帮助。

Here is a Fiddle to demonstrate my problem. 这是一个小提琴来演示我的问题。

You could do this with just CSS: 您可以使用CSS来做到这一点:

tr:first-child img.rowUp { display: none }
tr:last-child img.rowDown { display: none }

http://jsfiddle.net/Q6Q8T/ http://jsfiddle.net/Q6Q8T/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM