开发者

How I set the 'top' css value as a variable using $(this)

How I set the 'top' css value as a variable using $(this) ?

Thus far I have not been able to do so开发者_开发知识库 with this code:

    $("#thumb").click(function () {

        var TopV = $(this).css('top');
        alert(TopV)
    });


That code snippet is attempting to read the CSS 'top' property, not write to it. It probably does not exist on most elements.

Did you mean that you wanted to set it to some value, like this?

$(this).css('top', '5px');

More info on the css method

You should also be aware that the 'top' property is only meaningful if the element it is set on doesn't have 'static' positioning - that is, if it has absolute, relative or fixed. Otherwise it's ignored: it won't do anything, and probably won't be readable.

More info on the top CSS property


var offset = $(this).offset();
$(this).html( "top: " + offset.top );

you can try position() also


What do you want to set it to? Something like this will do it:

   $("#thumb").click(function () {
        var targetTop = '100px';
        $(this).css('top', targetTop);
        var TopV = $(this).css('top');
        alert(TopV)
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜