2013-06-24 130 views
0

這是我的文件之一的輸出如何獲得動態HTML表內容

<table> 
<tr> 
    <td> Description 1 </td> <td> Content 1 </td> 
</tr> 
<tr> 
    <td> Description 2 </td> <td> Content 2 </td> 
</tr> 
<tr> 
    <td> Description 3 </td> <td> Content 3 </td> 
</tr> 

我想說明X與PHP的內容,然後我想讀出量x。我的問題之一是,在拍攝內容之前,我不知道我有多少個description-td。

DOM/getElementsBy應該爲我的問題工作嗎?

最好的問候, 蘇珊

+0

這取決於如果你想這樣做對服務器(PHP)或客戶端(JavaScript),但是,是的,它應該 - 但這是一個非常普遍的問題的非常普遍的答案。 – Voitcus

+0

我會在服務器端進行。是的,我只需要一個普遍的答案。我想知道如何最有效地做到這一點:) – Susanne92

+0

你可以在簡單的或foreach循環中做到這一點。只需在迴路中回顯每一行。 – Voitcus

回答

1

在這裏,你有使用jQuery的解決方案。 Try if yourself 在這個例子中,我們顯示了在警告對話框說明數的含量3

HTML:

<table id="mytable"> 
    <tr> 
     <td> Description 1 </td> <td> Content 1 </td> 
    </tr> 
    <tr> 
     <td> Description 2 </td> <td> Content 2 </td> 
    </tr> 
    <tr> 
     <td> Description 3 </td> <td> Content 3 </td> 
    </tr> 
</table> 

JQUERY:

// Search content of description number 3 
$(document).ready(extractContent(3)); 

function extractContent(num) { 

    $('td', $('#mytable')).each(function() { 
     // The text in the first column must match " Description num " 
     if ($(this).text() == " Description " + num + " ") 
      alert("Content found: "+$(this).next().text()); 
    }); 
} 
+0

非常感謝!祝你有美好的一天! – Susanne92

+0

不客氣! – maqjav