jQuery添加到css元素

bba*_*bba 1 javascript css jquery

我有我元素的id.我需要将一个给定值(由javascript中的变量确定)添加到元素的css属性之一.即给出一个元素id=my_id如何添加x到该元素的margin-top属性?

Bjö*_*örn 5

var x = 20;
$('#my_id').css('margin-top', function(index, value) {
    if (isNaN(parseInt(value)))
        return x;

    return parseInt(value) + x
});
Run Code Online (Sandbox Code Playgroud)