javascript - Using shorthand assignment in jQuery for element attributes - Stack Overflow

To double the width of the img, i can do this in jQuery:<img src='blah.jpg' id='pic&#

To double the width of the img, i can do this in jQuery:

<img src='blah.jpg' id='pic' />

$('#pic').height($(this).height()*2);

that works fine, but i really like to use shorthand assignments like:

var count = 5;
count *= 2; // to get 10.

Since element.height returns the height function in jQuery, i can't use shorthand assignments. Is there no way to do shorthand assignments in jQuery for element attributes?

To double the width of the img, i can do this in jQuery:

<img src='blah.jpg' id='pic' />

$('#pic').height($(this).height()*2);

that works fine, but i really like to use shorthand assignments like:

var count = 5;
count *= 2; // to get 10.

Since element.height returns the height function in jQuery, i can't use shorthand assignments. Is there no way to do shorthand assignments in jQuery for element attributes?

Share Improve this question edited Jun 4, 2012 at 5:32 nnnnnn 150k30 gold badges209 silver badges247 bronze badges asked Jun 4, 2012 at 5:21 Ray ChengRay Cheng 12.6k16 gold badges78 silver badges139 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 13

You can modify the attributes such as height without using jQuery

document.getElementById("pic").height *= 2;

Or if you want to select the element with jQuery you can use this snippet. It selects the element and then accesses it directly using the indexer.

$("#pic")[0].height *= 2;
$('#pic').height($('#pic').height() * 2);

or

  $('#pic')[0].height *= 2;

Answers question of how to do it in jQuery

$('#pic').height(function( i, ht){
   return ht *2;
});

See API added function in 1.4.1 http://api.jquery./height/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744796766a4594251.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信