2012-12-19 44 views
3

我想用JQuery替換一整行,但似乎不工作(行不被替換)。這裏是鏈接到示例代碼:http://jsfiddle.net/s2kwb/Jquery/JS:替換表中的一行

的JavaScript:

$("td.99999").first().parent().next().replaceWith("<tr><td >category 3</td> 
<td >2222</td> 
<td >something 22</td> 
<td >something 22</td> 
<td >$3,433</td> 
<td >$300</td> 
<td >$3,733</td> 
<td >$349</td> 
<td >$4,082</td> 
</tr>");​ 

HTML:

<table border="1"> 
<tr > 
    <th>category</th> 
    <th>rank</th> 
    <th>priority</th> 
    <th>contact</th> 
    <th>price</th> 
    <th>tax</th> 
    <th>total price</th> 
    <th>shipping</th> 
    <th>Net payment</th> 
</tr> 
<tr class="displaytagOddRow"> 
    <td class="99999">category 1</td> 
    <td class="99999">99999</td> 
    <td class="99999">something</td> 
    <td class="99999">something</td> 
    <td class="99999 alignRight">$3,433</td> 
    <td class="99999 alignRight">$300</td> 
    <td class="99999 alignRight">$3,733</td> 
    <td class="99999 alignRight">$349</td> 
    <td class="99999 alignRight">$4,082</td> 
</tr> 
<tr class="displaytagOddRow" style="Background-color:Red"> 
    <td class="3333">category 2</td> 
    <td class="3333">3333</td> 
    <td class="3333">something</td> 
    <td class="3333">something</td> 
    <td class="3333 alignRight">$3,433</td> 
    <td class="3333 alignRight">$300</td> 
    <td class="3333 alignRight">$3,733</td> 
    <td class="3333 alignRight">$349</td> 
    <td class="3333 alignRight">$4,082</td> 
</tr> 
</table> 

我在做什麼錯?

在此先感謝。

回答

5

終止每行的字符串。另請參閱updated jsFiddle

$("td.99999").first().parent().next().replaceWith("<tr><td >category 3</td>" + 
    "<td >2222</td>" + 
    "<td >something 22</td>" + 
    "<td >something 22</td>" + 
    "<td >$3,433</td>" + 
    "<td >$300</td>" + 
    "<td >$3,733</td>" + 
    "<td >$349</td>" + 
    "<td >$4,082</td>" + 
    "</tr>");​ 

Google JavaScript Style Guide建議這個字符串連接,並建議對了替代的反斜槓換行符多行字符串。

whitespace after the slash will result in tricky errors

+0

**謝謝你這麼多** ** – Patty

2

修復這些錯誤在你的小提琴和添加的jQuery作爲參考使它工作:

DEMO

你的字符串的不正確的字符串常量。在瀏覽器調試工具的控制檯還指出:

SyntaxError: unterminated string literal [Break On This Error]

$("td.99999").first().parent().next().replaceWith("category 3

固定腳本:

$("td.99999").first().parent().next().replaceWith("<tr><td >category 3</td><td >2222</td><td >something 22</td><td >something 22</td><td >$3,433</td><td >$300</td><td >$3,733</td><td >$349</td><td >$4,082</td></tr>"); 
+0

這工作too.thank you.Realized的換行符是問題** – Patty