开发者

Using the jQuery width() and height() methods

开发者 https://www.devze.com 2022-12-11 11:13 出处:网络
Can you use the .width() and .height() methods to inc开发者_如何转开发rease the height like $(\'x\').width(\'+=2px\') or $(\'x\').height(\'+=2px\')?

Can you use the .width() and .height() methods to inc开发者_如何转开发rease the height like $('x').width('+=2px') or $('x').height('+=2px')?

I couldn't find any information about this on the jQuery website, but I also haven't seen anything to suggest that this doesn't work.


These methods get and set the height or width of the elements.

width() or width(val)

Such approach as you say is only provided on .animate()

Sinan.


Why not just do something like this?

var el = $('x');
el.width(el.width()+2);

EDIT: To clean it up, you can create a plugin, something like this (untested):

jQuery.fn.increaseWidth = function(amount) {
  this.width(this.width()+amount);
  return this;
};

This would allow you to do $('x').increaseWidth(2)


No. The value you pass to width() is expected to be a CSS length value. Pixels are assumed if you don't specify a unit.

0

精彩评论

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