我正在尋找替換表,它是行和單元格與div,但我發現的腳本替換每個子表和他們的後代與div,我不想。子表應該保留表格。用div替換頂級父表元素,而不是子表
我該如何定位頂級父表而不是任何子表?
$('table.calendar-header').each(function(){
$(this).replaceWith($(this).html()
.replace(/<tbody/gi, "<div class='table'")
.replace(/<tr/gi, "<div class='tr'")
.replace(/<\/tr>/gi, "</div>")
.replace(/<td/gi, "<div class='td")
.replace(/<\/td>/gi, "</div>")
.replace(/<\/tbody/gi, "<\/div")
);
});
我試過插入:第一個孩子和.first()到各個地方的腳本無濟於事。
以下是運行腳本之前HTML的樣子[請注意,我忽略了此代碼段中子表的內容]。您可以看到父表中有表。我希望那些保持原樣,不能被取代。
<table class="calendar-header" cellspacing="0" cellpadding="6" border="0" style="">
<tbody>
<tr>
<td valign="top">
<table class="calendar" cellspacing="0" cellpadding="2"></table>
</td>
<td align="center">
<div class="eventNav"></div>
</td>
<td valign="top" align="right">
<table class="calendar" cellspacing="0" cellpadding="2"></table>
</td>
</tr>
</tbody>
</table>
http://api.jquery.com/replacewith/ – aahhaa
我已經閱讀了replaceWith()上的jquery頁面,但是我沒有看到任何關於僅針對父項目或排除子項的建議。謝謝。 – david
你正在做一個字符串替換?因爲當你調用'html()'時,它會返回該標籤內的所有字符串,而你只是用新字符串替換所有的tbodys。 – iWantSimpleLife