2010-07-03 64 views
0

這是我的HTML頁面。如果您將此完整的HTML複製粘貼到瀏覽器中,您會看到第二部分具有元素和持續時間,而第三部分具有元素和目標資源,在這種情況下,我想動態地改變第二行和第三行以像第一行一樣展開。使用jQuery遞歸地在選擇器上應用colspan屬性

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> New Document </TITLE> 
<META NAME="Generator" CONTENT="EditPlus"> 
<META NAME="Author" CONTENT=""> 
<META NAME="Keywords" CONTENT=""> 
<META NAME="Description" CONTENT=""> 
<script type="text/javascript" src="jquery-1.4.2.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
    }); 
    </script> 

</script> 

</HEAD> 

<BODY> 
<table border="1"> 
<tr> 

<th class="element_header">Elements1</th> 
<th class="tr_res">TR1</th> 
<th class="duration">Duration1</th> 

</tr> 

<tr> 

<td class="element_td">Element Data 1</td> 
<td class="tr_td">TR - TD - 1</td> 
<td class="duration">Duration Data 1</td> 

</tr> 


<tr> 
<th class="element_header">Elements2</th> 
<th class="duration">Duration2</th> 
</tr> 
<tr> 
<td class="element_td">Element Data 2</td> 
<td class="duration">Duration Data 2</td> 
</tr> 


<tr> 
<th class="element_header">Elements3</th> 
<th class="tr_res">TR3</th> 

</tr> 
<tr> 
<td class="element_td">Element Data 3</td> 
<td class="tr_td">TR - TD - 3</td> 
</tr> 
</BODY> 
</HTML> 

回答

0

我會嘗試更改您的選擇器是基於類而不是id。使用相同ID的多個控件不是很好的做法。加上jQuery只選擇一個單一的項目時,選擇ID。

另外我會檢查length屬性而不是布爾值false,因爲jquery將始終有一個對象。因此,將您的支票更改爲.length> 0.

+0

亞..你是對的。我會改變我的選擇器使用類以來使用。向我們提供了具有element_th和element_td的標籤的總數,但您的意思是針對目標資源的布爾檢查,並且持續時間也必須使用長度進行檢查?這將是geart,如果你可以幫助我與僞代碼或可能是一些真正的代碼因爲我一直在抓我的頭與此...謝謝! – 2010-07-03 12:06:24

+0

是的,你應該看起來像這樣: var hasTargetedResources = $(「。targeted_resources_header」); var hasDuration = $(「。duration_header」); if(hasTargetedResources.length> 0 || hasDuration.length> 0) 因此,你顯然會根據你的邏輯調整計數。 此外,我假設你寫或在那個語句,如果它只是僞代碼,或在JS實際上||。只是爲了以防萬一。 – spinon 2010-07-03 14:32:42

+0

是的,這是一個僞代碼。在JS中,我們有一個||條件是這樣的...但問題仍然存在,我是否需要有一個for循環遍歷所有元素,並檢查是否有針對性的資源和持續時間存在? – 2010-07-03 15:31:33