2014-07-09 238 views
0

表Css属性不对齐,如果我在jQuery中给CSS。 首先,我在这里只给的Html动态表Css属性使用jQuery

<tr id="trCustomLabel"> 
<td class="font12" style="border: 0px; width: 8%;padding-left: 10px;" id="tdlab">Custom Label</td> 
<td class="font11" style="border: 0px; text-align: center; width: 2%;" id="tdlab1">:</td> 
<td class="font12" style="border: 0px; padding-top: 0px; width: 55%;" id="tdlab2"> <div><input type="checkbox" id="chkRestoreLocation"/> Create new label for restore</div></td> 
</tr> 

后,我在jQuery的controling元素

if (currentID == "tabGmail") {$('tr[id$=trCustomLabel]').css("display", "block");} 
if (currentID == "tabDrive") { $('tr[id$=trCustomLabel]').css("display", "block");} 
if (currentID == "tabContact") {$('tr[id$=trCustomLabel]').css("display", "none");} 

输出: enter image description here

+1

,最好添加一个动态类,而不是实际的CSS的 – yuvi

+0

@Michiel是的,我如果你只是想隐藏和显示元素,然后使用'.hide()'和'.show()'函数 – arun

回答

0

请这改变了你的代码。

if (currentID == "tabGmail") { 
     $('tr#trCustomLabel').css("display", "block"); 
    } 
    if (currentID == "tabDrive") { 
     $('tr#trCustomLabel').css("display", "block"); 
    } 
    if (currentID == "tabContact") { 
     $('tr#trCustomLabel').css("display", "none"); 
    } 
+1

。 。 – amol

+0

或'toggle()'来控制元素,而标签单击它将错误对齐 – yuvi

0
switch (currentID) { 
    case tabGmail: 
     $('tr#trCustomLabel').css("display", "block"); 
     break; 
    case tabDrive: 
     $('tr#trCustomLabel').css("display", "block"); 
     break; 
    case tabContact: 
     $('tr#trCustomLabel').css("display", "none"); 
     break; 
} 
0

我要去的肢体和猜你有一堆的控制表行的显示按钮。我还假设trCustomLabel是一个占位符(因为你不是真的在不同的时间使用同一个id,对吗?这有点违背了id的意思,如果你想要定位一些不同的元素,可以使用一个类)。

为了减少代码行,也让它稍微更通用的,我建议你做这样的:

// define what points to where 
var controllers = { 
    'tabGmail' : 'trGmailLabel', 
    'tabDrive' : 'trDriveLabel', 
    'tabContact': 'trContactLabel' 
} 

// toggle the required element 
if (controllers.hasOwnProperty(currentID)) { 
    $('tr#' + controllers[currentID]).toggle(); 
}