开发者

jQuery - How to get elements height value and use it in another element via css code?

I want to get height ( or width.开发者_如何学编程. doesnt really matter ) from element A and use that height to set css to element B ...like

$('#element_B').css({ 'top','element_A_height' })

A little example here.. I know the javascript is not even close to something that works, I just tried to explain what i wanted to happen..

http://jsfiddle.net/cJdXg/

I have no idea how to achieve this.. any ideas?


Here's an example of what you want to achieve: http://jsfiddle.net/cJdXg/2/

HTML:

<div id="BoxeeBox">Lorem ipsum dolor. #boxeeBox</div>
<div id="emptyBox1">#emptyBox1</div>
<div id="emptyBox2">#emptyBox2</div>

JavaScript:

var BoxeeBox = $('#BoxeeBox'); /* cache the selector */

$('#emptyBox1').css({ width: BoxeeBox.width() });
$('#emptyBox2').css({ height: BoxeeBox.height() });

CSS:

div { background: #b6d754; color: #ffffff; padding: 10px; margin: 5px; float: left; clear: both; border-radius:10px; }


What you're after is this:

http://api.jquery.com/height/

Example:

var elementAheight = $('#elementA').height();

however, if elementA has margins you may want to us this:

http://api.jquery.com/outerHeight/

Example:

var elementAoutheight = $('#elementA').outerHeight();

Hope it helps!


have u seen these functions and examples given below the functions, this might helps u

http://api.jquery.com/height/

http://api.jquery.com/width/


$('#element_B').css("top", $("#element_a").css("top"));

But i suggest to cache it first, and reuse for other case

var topVal = $("#element_a").css("top");
 $('#element_B').css("top", topVal);
 $('#element_C').css("top", topVal);


it should be something like

$("#elemA").height( $("#elemB").height() );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜