better way to jQuery set outerWidth(true) - width
In short, is there a way to set the outerWidth?
$.outerWidth(true, newValue)
Update: I found some addons that do this, although I need one function defini开发者_开发知识库tion not 3 addons to do this.
You were pretty close. With jQuery 1.8+, just use:
.outerWidth(newValue, true);
Also works for:
.innerWidth(val)
.innerHeight(val)
.outerWidth(val [, true])
.outerHeight(val [, true])
It actually became a setter with jQuery 1.8. That is 1 year after you posted your question. Moreover, it was listed in the changelog, but nobody updated the documentation which explains that it might have been missed by many.
If you have jQuery 1.7.2 or older, see Xeo's answer (on this page) or use jquery-ui 1.8.4+ which automatically enhances those methods into getters/setters (since in jQuery 1.8, those methods were actually ported from jquery-ui).
Is this what you're looking for?
var a = $('element');
actualWidth = a.outerWidth(true),
desiredWidth = 1000,
difference = desiredWidth - actualWidth,
a.css('width',difference);
If you want to set the width of an element, you can use these examples
var a = $("#element");
a.width(10);
a.css("width", "10px");
精彩评论