I have jquery code like this:
$(".element").live('click', function(e) {
$(".element").cs开发者_StackOverflow中文版s("height", $(this).parent().height() +"px") ;
});
What i want is:
Change the height of all ".element"s to the height of their parent (different for each one). The problem is, that the $(this) tag refers to the element that has been clicked on (dummy test function, will change later). I tried using $(".element").parent().height(), but this seems not to be right either. How do i refer to correct actual element?
If you mean that clicking one .element
should change the height of all .element
s to the height of their respective parent, try this:
$(".element").live('click', function(e) {
$(".element").each(function(){
$(this).css("height", $(this).parent().height() +"px");
});
});
You should have no trouble if you do it like this:
$(".element").live('click', function(e) {
$(this).css("height", $(this).parent().height() +"px") ;
});
You're right that $(this)
refers to the element that was clicked on, so you have to use this
in all cases. You want to change the element that was clicked on's height ( this's height ) to the element that was clicked on's parent height ( this's parent height ), right?
The following code will, on the click on any DOM object with the class element, iterate through all elements and snap them to parent height.
$(".element").live('click', function(e) {
// go through each object with class element
$(".element").each(function() {
$(this).css("height", $(this).parent().height() +"px") ;
});
});
精彩评论