我建议:
function idProgress (e) {
var self = this,
id = self.id,
next = self.nextElementSibling;
if (next === null) {
// this assumes you don't want to remove the last element
return false;
}
else {
self.parentNode.removeChild(self);
next.id = id;
next.addEventListener(e.type, idProgress);
}
}
var span = document.getElementById('current');
span.addEventListener('click', idProgress);
JS 小提琴演示。
要删除所有span
元素单击(包括最后一个):
function idProgress (e) {
var self = this,
id = self.id,
next = self.nextElementSibling;
self.parentNode.removeChild(self);
if (next !== null) {
next.id = id;
next.addEventListener(e.type, idProgress);
}
}
var span = document.getElementById('current');
span.addEventListener('click', idProgress);
JS 小提琴演示。
使用keypress
而不是click
(我在第一次阅读问题时忽略了):
function idProgress (e) {
var self = this.getElementById('current'),
id = self.id,
next = self.nextElementSibling,
key = e.which;
if (key !== 110 || next === null) {
return false;
}
else {
self.parentNode.removeChild(self);
next.id = id;
}
}
document.addEventListener('keypress', idProgress);
JS 小提琴演示。
或者:
function idProgress(e) {
var self = this.getElementById('current'),
id = self.id,
next = self.nextElementSibling,
key = e.which;
if (key == 110) {
self.parentNode.removeChild(self);
if (next !== null) {
next.id = id;
}
}
}
document.addEventListener('keypress', idProgress);
JS 小提琴演示。
参考: