2010-06-01 98 views
0

嘗試使用scriptaculous進行拖放操作的示例代碼片段。它工作正常IE8但Firefox和Chrome產生的錯誤「sections.each不是一個函數」使用javascript/scriptaculous獲取'sections.each不是函數'

下面是代碼:

function getGroupOrder() { 
    var sections = document.getElementsByClassName('section'); 
    var alerttext = ''; 
    sections.each(function(section) { 
     var sectionID = section.id; 
     var order = Sortable.serialize(sectionID); 
     var mySectionID = Right(section.id); 
     var myLen = String(Sortable.sequence(section)).length; 
     var StuCode = ""; 
     if (myLen ==8) 
     {var StuCode = String(Sortable.sequence(section)).substring(myLen, 2);} 
     else if (myLen ==9) 
     {var StuCode = String(Sortable.sequence(section)).substring(myLen, 3);} 

     alerttext += mySectionID + ': ' + StuCode + '\n'; 
      alerttextb = sectionID + ': ' + StuCode + '\n'; 
    } 
} 

一種解決方案在一個論壇上提出「我能解決這個問題通過用$ A()包裝調用document.getElementsByClassName('section');但我不知道這意味着什麼!我問了這是什麼意思,但這個帖子是在2008年提出的,目前還沒有答覆。

感謝您提供任何幫助。

問候

回答

2

getElementsByClassName方法上本機實現返回一個NodeList,而不是一個對象Array

從PrototypeJS的$A方法的任何迭代的對象轉換成Array對象,例如:

$A(sections).each(function(section) { 
    //... 
}); 
+1

綜觀文檔,我看到['getElementsByClassName'](HTTP://www.prototypejs .org/api/element/getElementsByClassName)方法在Prototype 1.6中已被棄用,他們現在建議使用['$$'](http://www.prototypejs.org/api/utility/dollar-dollar)或[ 'Element#select'](http://www.prototypejs.org/api/element/select)對於我說的事實,'getElementsByClassName'的本地實現返回'NodeList'對象... – CMS 2010-06-01 07:50:12

+0

謝謝你,生病了如果我更新,請注意。 – tonyyeb 2010-06-01 09:19:33

相關問題