2011-05-24 82 views
0

如何將#book的寬度設置爲#clickme a的寬度?如果我在回調函數中說$(this).width(),它會給我#book的寬度。#clickme中有多個錨標籤。我需要在動畫結尾處增加寬度,一看讓我知道如何在jQuery中設置元素的寬度

$('#clickme a').click(function() { 
    $('#book').animate({ 
    opacity: 0.25, 
    left: '+=50', 
    height: 'toggle' 
    }, 5000, function() { 
    //set width of #book to width of #clickme a item 
    }); 
}); 

謝謝

回答

1
$('#clickme a').click(function() { 
    var aWidth = $(this).css("width");//alert(aWidth); 
    $('#book').animate({ 
     opacity: 0.25,  left: '+=50',  height: 'toggle' }, 500, 
     function() { 
      //set width of #book to width of #clickme a item 
      //alert($(this).css("width")); 
      $(this).css("width",aWidth); 
     }); 
}); 
4

基於OP評論更新時間:

$("#clickme a").click(function() { 
    var clicked = $(this); 
    $("#book").animate({ 
     opacity: 0.25, 
     left: "+=50", 
     height: "toggle" 
    }, 5000, function() { 
     $(this).css("width", clicked.width()) 
    }); 
}); 
+0

我有不止一個的#clickme,將它仍然工作? – manraj82 2011-05-24 15:06:30

+0

'a'對應'#book'嗎?每個'a'裏面的每個'#book'? – 2011-05-24 15:10:17

+0

#clickme是一個相對的div,錨標籤位於#clickme中絕對定位的ul,所以是#book [絕對位於#clickme] – manraj82 2011-05-24 15:14:06

1
$('#clickme a').click(function() { 
    var _w = $(this).width(); 
    $('#book').animate({ 
    opacity: 0.25, 
    left: '+=50', 
    height: 'toggle' 

    }, 5000, function() { 
    $(this).width(_w); 
    }); 
}); 
+0

我需要增加動畫結尾處的寬度 – manraj82 2011-05-24 15:09:26

0

你可以嘗試這樣的...

$('#clickme a').click(function() { 
    $('#book').animate({ 
    opacity: 0.25, 
    left: '+=50', 
    height: 'toggle' 
    }, 5000, function() { 
    $(this).css('width','500'); 
    }); 
}); 
1
$('#book').width($("#clickme a").width()) 
+1

這不做任何事。這是書。 – Lobo 2011-05-24 15:14:49

+0

對不起,可能會出現問題,如果有多個點擊我的元素我猜:/ – Treemonkey 2011-05-24 15:19:00

相關問題