2009-07-10 8 views
1

我想用Prototype的新元素函數構造一個表。當我用完整的內容更新thead時,我遇到了Firefox中的問題:所有元素和內容。 Firefox正在剝離標籤並僅顯示內容。Prototype Element.update多個對象

無論如何,我決定構造每一個th元素,然後使用Element.update()函數將它附加到thead。但是我還沒有找到用這個函數追加多個對象的方法。

的th元素看起來像這樣:

var thead_amount = new Element('th', { 
    'id': 'amount' 
}).update('amount'); 

這工作正常:

new Element('thead').update(thead_amount); 

此輸出與上述相同的:

new Element('thead').update(thead_amount, thead_pdf, thead_tags, thead_date, thead_options); 

此輸出[object HTMLTableCellElement][object HTMLTableCellElement][object HTMLTableCellElement][object HTMLTableCellElement][object HTMLTableCellElement]

new Element('thead').update(thead_amount + thead_pdf + thead_tags + thead_date + thead_options); 

如何在Prototype的update()函數中附加多個對象?

謝謝!

+0

我也嘗試了innerHTML,但在Firefox中沒有改變!thead應該是這樣的: amount標籤日期附件 但它只能說明: amounttagsdateattachment在Firefox 非常怪異的行爲。它剝去標籤或不關閉它們.. 我找不到解決方案:( – richard 2009-07-20 15:05:49

回答

1

編輯

它只是跳了出來,在我要添加「TH」元素,以一個「THEAD」。這不好! THEAD應該只包含TR。 TR的可以包含TH的,但是如果你使用THEAD,我會使用TD的。

請記住:tbody,thead和tfoot是表格的細分,並且必須包含tr元素的。你不應該直接將td或th元素放入這些元素中,因爲結果最好是不可預測的。

結束編輯

的這裏的問題是,Element.update()將被傳遞的字符串,HTML代碼段,或實現的toString javascript對象(例如元)。但是,Element不支持「+」運算符,因爲它使用它,並將所有對象名稱相加。你將不得不顯式調用每個孩子的toString方法這樣:如果你在你的應用程序中使用script.aculo.us(原型擴展)

new Element('thead').update(thead_amount.toString() 
    + thead_pdf.toString() 
    + thead_tags.toString() 
    + thead_date.toString() 
    + thead_options.toString()); 

,您可以使用生成器類,以協助更容易元素建設。它提供了一個更直觀的界面,特別是在創建大量元素時。下面是一個例子:

var table = Builder.node('table', { 
    width: '100%', 
    cellpadding: '2', 
    cellspacing: '0', 
    border: '0' 
}); 

var tbody = Builder.node('tbody'), 
    tr = Builder.node('tr', { className: 'header' }), 
    td = Builder.node('td', [Builder.node('strong', 'Category')]); 

tr.appendChild(td); 
tbody.appendChild(tr); 
table.appendChild(tbody); 

$('divCat').appendChild(table); 

查看詳情http://wiki.github.com/madrobby/scriptaculous/builder

+0

Scriptaculous'Builder功能非常好!我正在尋找的東西,非常感謝!也感謝您清理那些thead和th,實際上我之前使用過tr,但是我的一個朋友給了我一個錯誤的建議:P現在在FF中完美無缺,因爲它沒有創建節點:)謝謝! – richard 2009-07-21 21:21:37