2011-08-04 18 views
0

由於阿賈克斯加載的內容,我不得不改變功能:PrototypeJS Element.prototype.appendChild問題

document.observe('dom:loaded',function() {  
    $$('.userListHeaderCell').invoke('observe', 'click', function() {  
     this.toggleClassName('desc');   
     var colnumber = this.up().childElements().indexOf(this);   
     var content = this.up(2); 
     sortColumn(content, colnumber, this.hasClassName('desc'));  
    }); 
}); 

TO:

document.observe('click', function(e, el) { 
    if (el = e.findElement('.userListHeaderCell.sortable')) { 
     el.toggleClassName('desc');   
     var colnumber = el.up().childElements().indexOf(el);   
     var content = el.up(2); 
     sortColumn(content, colnumber, el.hasClassName('desc')); 
    } 
}); 

的sortColumn樣子:

function sortColumn(content, colnumber, desc) {  
    content.select('.userListRow').sort(function(a,b){   
     var atext = a.down(colnumber).innerHTML.stripTags().toLowerCase();   
     var btext = b.down(colnumber).innerHTML.stripTags().toLowerCase();   
     return atext.localeCompare(btext) * (desc ? -1 : 1);  
    }).each(Element.prototype.appendChild, content); 
} 

切換和colnumber工作和內容是一個對象HTMLDivElement,但似乎我需要轉換內容變量或使用其他r函數,但不知道sortColumn中的內容。我得到的錯誤是:

「無法轉換JavaScript引」 nsresult: 「0x80570009(NS_ERROR_XPC_BAD_CONVERT_JS)」 的位置: 「JS框架:: https://www.someurl.com/scripts/prototype.js :: ::行804」 的數據:無]

做什麼我需要改變才能完成這項工作?

謝謝。

編輯:

在IE錯誤顯示: 無法獲取財產「呼叫」的值:對象爲空或未定義的prototype.js,線路804字符9

我相信這個問題是Element.prototype.appendChild

試過添加Element.extend(內容),但似乎沒有幫助。

示例數據:

<div id="contentbox_Users" class="userList"> 
    <div class="userListHeader"> 
     <div class="userListHeaderCell col1 sortable">First Name</div> 
     <div class="userListHeaderCell col2 sortable">Last Name</div> 
    </div> 
    <div id="contentbox_People"> 
     <div class="userListRow"> 
      <div class="userListCell col1">John</div> 
      <div class="userListCell col2">Smith</div> 
     </div> 
     <div class="userListRow"> 
      <div class="userListCell col1">Bob</div> 
      <div class="userListCell col2">Ray</div> 
     </div> 
     <div class="userListRow"> 
      <div class="userListCell col1">Fred</div> 
      <div class="userListCell col2">Jones</div> 
     </div> 
    </div> 
</div> 
+0

如果沒有HTML,很難判斷腳本是否選擇了正確的元素。像這樣的錯誤通常意味着元素無法找到。 –

+0

發佈了HTML,謝謝。 – fanfavorite

+0

在CSS上爲「desc」放置一個邊框,並在添加類名稱時查看選定的元素。看到發生了什麼可能會有所幫助。 –

回答

1

既然是我的代碼失敗我應該有一展身手。

線,

}).each(Element.prototype.appendChild, content); 

是說的簡寫方式,

}).each(function(row) { 
    content.appendChild(row); 
}); 

你可以嘗試這種方式,看看情況有所好轉。我不認識這個錯誤,但是一個快速的谷歌表明它來自Firefox並且涉及到將一個非元素傳遞給一個預期的元素。在這種情況下,我傾向於在任何地方都使用很多撥打console.log(...)的電話(並在Firefox中按F12),然後看看會發生什麼。記錄數組應該將所有行顯示爲HTML元素。

+0

謝謝,這是行得通的,但它將外部行放在與contentbox_Users相同的級別上。我可以解決這個問題。 – fanfavorite