[英]Can I use Jquery to find an element and set it's CSS width to current-width - x?
Say I need to correct a number of elements widths by 19px. 假设我需要将多个元素的宽度校正为19px。
Is it possible to find
elements and set the found elements CSS-width like so: 是否可以
find
元素并设置找到的元素CSS宽度如下:
$el.find( '.ui-content' )
.css({'width': "parseFloat(found_element_current_width) -19" })
If not, what's the best way to do this? 如果没有,最好的方法是什么?
Yes. 是。
$el.find( '.ui-content' )
.width(function(i, width) { return width - 19; })
You can use css
's function: 你可以使用
css
的功能:
$el.find( '.ui-content' )
.css('width', function(i, w){
return (parseFloat(w) - 19) + 'px'
})
From the documentation of the .css()
function: 从
.css()
函数的文档:
As of jQuery 1.6,
.css()
accepts relative values similar to.animate()
.从jQuery 1.6开始,
.css()
接受类似于.animate()
相对值。 Relative values are a string starting with+=
or-=
to increment or decrement the current value.相对值是以
+=
或-=
开头的字符串,用于递增或递减当前值。 For example, if an element's padding-left was 10px,.css( "padding-left", "+=15" )
would result in a total padding-left of 25px.例如,如果元素的填充左边是10px,则
.css( "padding-left", "+=15" )
将导致25px的总填充左边。
So you could just do: 所以你可以这样做:
$el.find( '.ui-content' )
.css('width', '-=19');
yes you can. 是的你可以。
$el.find( '.ui-content' )
.css('width', parseInt(yourwidth) - 19)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.