2014-02-28 34 views
0

漫长的一天,所以最有可能我失去了一些愚蠢的东西,但我想要做的是循环表获取其宽度,然后设置父元素的宽度,但不工作任何想法或看到哪里出错了?jQuery - 通过表循环,获取宽度和设置父类

HTML

<div class="vScroll"> 

    <table class="table-scroll"> 
    .... 
    </table> 

    <div class="box-bottom">stuff here</div> 

</div> 

<div class="vScroll"> 

    <table class="table-scroll"> 
    .... 
    </table> 

    <div class="box-bottom">stuff here</div> 

</div> 

<div class="vScroll"> 

    <table class="table-scroll"> 
    .... 
    </table> 

    <div class="box-bottom">stuff here</div> 

</div> 

JS

jQuery('.vScroll .table-scroll').each(function(i,el) { 
    var $tableSize = jQuery(this).width(); 
    jQuery(this).parent('.box-bottom').css('width',$tableSize); 
}); 

提前感谢!

+1

其他.table-scroll? – SarathSprakash

+1

'table-class'应该是'table-scroll'吗? –

+0

抱歉是的,我的错误 –

回答

0

试试这个

$('.vScroll .table-scroll').each(function(i,el) { 
    var $tableSize = $(this).width(); 
    $('.box-bottom', $(this).parent()).css('width',$tableSize); 
}); 

好像你是从

var $tableSize = $(this).width(); 

得到什么不正是你所需要的。它将返回包含该表的父div的宽度。如果你真的想选择的表,而不是DIV,使用

$('.table-scroll').each(function(i,el) { 
+0

谢谢,工作过! –

0

有一些小的改动,我觉得我纠正你的JS代码,并创造了什么,我相信是一个工作小提琴

http://jsfiddle.net/Xeb4S/2/

从本质上讲,这是导致JS

$('.vScroll .table-class').each(function() { var $tableSize = $(this).width(); $(this).siblings('.box-bottom').css('width',$tableSize); });

有一些改变,成为内地在HTML中添加一些内容并在选择器中给出正确的类名称 - 但是如果不使用它们,也不需要将参数传递给.each(),并且将jQuery引用简化为$

我也用siblings直接在下面设置.box-bottom项目的宽度?这是一个轻微的假设。你提到“设置父项的宽度” - 但.box-bottom项是兄弟姐妹,而不是父母?

希望这会有所帮助。