开发者

Prototype replace method giving null error

I'm new to prototype, but fairly experienced with jQuery so it could just be I'm misunderstanding how prototype works. I'm trying to do the following

$$("tr.total_line td.total_cell").first().replace(newTotal);

but I'm getting TypeError: Cannot read property 'firstChild' of null

When I execute $$("tr.total_line开发者_如何学编程 td.total_cell").first() in the JS console I get a DOM element result.

Here's the relevant markup

<tr class="total_line">
    <td colspan="2">Total</td>
    <td class="total_cell">$50.00</td>
    <td></td>
</tr>


Since .first() is returning a <td> element, you'll need to insert a <td> or <th> in order for the replacement to be valid HTML.

So if newTotal is:

"<td>$100</td>"

...it should work. But if it is just:

"$100"

...it doesn't.

Or another option would be to replace the innerHTML of the <td>:

$$("tr.total_line td.total_cell").first().innerHTML = newTotal;


If replace continues to cause problems you can try update instead which works in a similar way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜