普通的JavaScript與querySelectorAll
的那些和classList
支持:
var items = document.querySelectorAll('li.hide');
for (var i = 0; i < items.length; i++) {
items[i].classList.remove('hide');
items[i].classList.add('slide-down');
}
沒有querySelectorAll
:
var items = document.getElementsByTagName('li');
for (var i = 0; i < items.length; i++) {
if (items[i].classList.contains('hide')) {
items[i].classList.remove('hide');
items[i].classList.add('slide-down');
}
}
沒有querySelectorAll
和classList
:
var items = document.getElementsByTagName('li');
for (var i = 0; i < items.length; i++) {
if (new RegExp(/(?:^|\s)hide(?!\S)/g).test(items[i].className)) {
items[i].className = items[i].className.replace(/(?:^|\s)hide(?!\S)/g , '');
items[i].className += ' ' + 'slide-down';
}
}
來源
2015-10-04 23:59:49
iH8
'document.getElementsByClassName'返回'HTMLCollection',其行爲像一個「活」數組,而'document.querySelectorAll'返回一個非「活」NodeList'。 – royhowie