我正试图在CoffeeScript中重写它。不见了

时间:2010-11-17 17:32:15

标签: coffeescript

function getElementsByClassName(className)
{
 // get all elements in the document
 if (document.all)
 {
  var allElements = document.all;
 }
 else
 {
  var allElements = document.getElementsByTagName("*");
 }

 var foundElements = [];
 for (var i = 0, ii = allElements.length; i < ii; i++)
 {
  if (allElements[i].className == className)
  {
   foundElements[foundElements.length] = allElements[i];
  }
 }

 return foundElements;
}

var listItems = document.getElementsByClassName("quirky");

for (var i = 0, ii = listItems.length; i < ii; i++)
{
    alert(listItems[i].nodeName);
}

1 个答案:

答案 0 :(得分:6)

getElementsByClassName = (className) ->

  # get all elements in the document
  if document.all
    allElements = document.all
  else
    allElements = document.getElementsByTagName "*"

  el for el in allElements when el.className == className

# NOTE: getElementsByClassName was never assigned as a member
# of document. So this call will likely fail, unless you are
# using a latest-version browser.
listItems = document.getElementsByClassName "quirky"

for i in listItems
  alert i.nodeName