开发者

I'm trying to re-write this in CoffeeScript. Coming unstuck

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.getElementsByCla开发者_开发技巧ssName("quirky");

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


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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜