IE7環境下 querySelectorAll 不兼容的解決辦法

// 獲取所有的標籤,拿到所有的屬性(class同理)
function getAllElementsWithAttribute(attribute)
{
  var matchingElements = [];
  var allElements = document.getElementsByTagName('*');
  for (var i = 0, n = allElements.length; i < n; i++)
  {
    if (allElements[i].getAttribute(attribute) !== null)
    {
      // Element exists with attribute. Add to array.
      matchingElements.push(allElements[i]);
    }
  }
  return matchingElements;
}


// 用法 匹配<div show-controller="xxx"></div>
if(document.querySelectorAll) {
    var __node = document.querySelectorAll("[show-controller]");
} else {
	var __node = getAllElementsWithAttribute("show-controller")
}

有過一篇文章寫的也可以解決這種情況,但是着實不太好使

 

整理歸納了stackOverFlow的一篇問答

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章