开发者

Dynamic Padding (jQuery)

开发者 https://www.devze.com 2022-12-22 01:22 出处:网络
$(\'#menu .sub-right\').css( \'paddingLeft\', $(\'#menu .sub-left\').width() ); nor $(\'#menu .sub-right\').css({\'padding-left\': $(\'#menu .sub-left\').width()});
$('#menu .sub-right').css( 'paddingLeft', $('#menu .sub-left').width() );

nor

$('#menu .sub-right').css({'padding-left': $('#menu .sub-left').width()});
开发者_StackOverflow中文版

...these work. What am I doing wrong?

Thanks!


Does this work?

$('#menu .sub-right').css( 'padding-left', $('#menu .sub-left').width()+"px" );

The width function documentation states:

The difference between .css(width) and .width() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). The .width() method is recommended when an element's width needs to be used in a mathematical calculation.

But since CSS expects ###px, you can use the above code or this code:

$('#menu .sub-right').css( 'padding-left', $('#menu .sub-left').css('width') );

Either one should work.

0

精彩评论

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