對於加入活性類做下面
var liElems = document.getElementsByTagName('li'), mid = Math.floor(liElems.length/2);
Array.prototype.forEach.call(liElems, function (item, idx) {
item.idx = idx;
item.onclick = function() {
document.querySelector('li.active').classList.remove('active');
this.classList.add('active');
};
});
演示:Fiddle
var liElems = document.getElementsByTagName('li'),
length = liElems.length,
mid = Math.floor(liElems.length/2);
Array.prototype.forEach.call(liElems, function (item, idx) {
item.idx = idx;
item.onclick = function() {
document.querySelector('li.active').classList.remove('active');
this.classList.add('active');
if (liElems[mid].idx != mid) {
if (liElems[mid].idx == length - 1) {
this.parentNode.appendChild(liElems[mid])
} else {
this.parentNode.insertBefore(liElems[mid], liElems[liElems[mid].idx])
}
}
var pos = mid < this.idx ? mid : mid + 1;
this.parentNode.insertBefore(this, liElems[pos])
};
});
演示:Fiddle
其中元素的中間 – 2015-04-01 00:50:08
你要做的是...複製ul的html,編輯html,然後以你想要的方式將它附加回來。 – floor 2015-04-01 00:52:59