I am trying to set a block's height equal to its width with this $('.block').height($(this).width()); However it's not working properly for some reason. Please see an example: http://jsfiddle.net/Uy3YB/. What do I need to correct?

有帮助吗?

解决方案

Fiddle Demo

$('.block').height(function () {
    return $(this).width();
});

.height( function(index, height) )

A function returning the height to set. Receives the index position of the element in the set and the old height as arguments. Within the function, this refers to the current element in the set


Problem with your code

this below is the window object not $('.block')

$('.block').height($(this).width()); 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top