Javascript 如何从下拉列表中删除特定项?
用户如何从下拉列表中删除特定项目,而不是从所选项目及其以后的整个列表中删除Javascript 如何从下拉列表中删除特定项?,javascript,html5-canvas,Javascript,Html5 Canvas,用户如何从下拉列表中删除特定项目,而不是从所选项目及其以后的整个列表中删除 <label> History: <select id="historySelect"> </select> </label> <label> <input type="button" id="cmbDelete" value="Undo"> </label> var history = []; v
<label>
History:
<select id="historySelect">
</select>
</label>
<label>
<input type="button" id="cmbDelete" value="Undo">
</label>
var history = [];
var historySelect
historySelect = document.getElementById('historySelect')
historySelect.addEventListener('change', ()=>{
restoreHistoryAction(historySelect.value)
})
function drawCanvas() {
contextTmp.drawImage(canvas, 0, 0);
history.push(contextTmp.getImageData(0,0,canvasTmp.width,canvasTmp.height))
updateHistorySelection()
context.clearRect(0, 0, canvas.width, canvas.height);
}
历史:
var历史=[];
历史选择变量
historySelect=document.getElementById('historySelect')
historySelect.addEventListener('change',()=>{
restoreHistoryAction(historySelect.value)
})
函数drawCanvas(){
contextTmp.drawImage(画布,0,0);
history.push(contextTmp.getImageData(0,0,canvasTmp.width,canvasTmp.height))
updateHistorySelection()
clearRect(0,0,canvas.width,canvas.height);
}
下面是我用来将历史添加到下拉列表和撤消按钮的代码
function cmbDeleteClick(){
if(history.length<=1)
return
history.pop()
contextTmp.putImageData(history[history.length-1],0,0)
updateHistorySelection()
}
function updateHistorySelection(){
historySelect.innerHTML = ''
history.forEach((entry,index)=>{
let option = document.createElement('option')
option.value = index
option.textContent = index===0 ? 'Start ' : 'Action '+index
historySelect.appendChild(option)
})
historySelect.selectedIndex = history.length-1
}
function restoreHistoryAction(index){
contextTmp.putImageData(history[index],0,0)
}
cmbDelete = document.getElementById("cmbDelete");
cmbDelete.addEventListener("click",cmbDeleteClick, false);
函数单击(){
if(history.length{
let option=document.createElement('option')
option.value=索引
option.textContent=index==0?'Start':'Action'+索引
historySelect.appendChild(选项)
})
historySelect.selectedIndex=history.length-1
}
函数restoreHistoryAction(索引){
contextTmp.putImageData(历史[索引],0,0)
}
cmbDelete=document.getElementById(“cmbDelete”);
cmbDelete.addEventListener(“单击”,cmbDeleteClick,false);
如果只删除下拉列表中的选定项,这将是完美的 您可能正在寻找该方法 数组.拼接(开始[,删除计数[,项1[,项2[,…])) 这将从下拉列表中删除
selectIndex
,但我不确定画布逻辑是否正常工作,除非它按照您的预期工作
function cmbDeleteClick(){
if(history.length<=1) return;
var historyIndex = document.getElementById("historySelect").selectedIndex;
var historyItems = history.splice(historyIndex, 1);
contextTmp.putImageData(historyItems[0],0,0);
updateHistorySelection();
}
函数单击(){
if(history.length