将变量中保存的像素数量添加到jQuery中的css属性中

时间:2017-10-25 10:42:14

标签: javascript jquery html css

我想在jQuery中的css属性中添加一些存储在我的变量“x”中的像素。

我知道自从jQuery 1.6以来你可以轻松使用这样的东西:

$(this).css({top: "+=150"});

但是当我使用变量而不是150时,它不起作用,因为变量必须用引号写出。

$(this).css({top: "+= x"});

1 个答案:

答案 0 :(得分:1)

请试试这个

$(this).css({top: "+= "+ x +"px"}); 

代替你的代码