onKeyUp事件(不工作)?

时间:2014-11-12 15:48:04

标签: javascript html5 window addeventlistener confirm

我希望当我执行mouseup时,我可以选择删除或没有带有" window.confirm"的列表。控制台说" Uncaught ReferenceError:li未定义"。

var add =  document.getElementById('create-memo');

function newMemo(list, itemText){
    var listMemo = document.createElement('li');
    listMemo.className = 'memo';
    listMemo.innerText = itemText;
    list.appendChild(listMemo);

  listMemo.addEventListener('mouseup', removeList);
}

function removeList(){
    if (window.confirm('You want remove this memo?')) {
        li.parentNode.removeChild(listMemo);
    }
}

2 个答案:

答案 0 :(得分:1)

您要为{1}}提供关键字事件的参数是.addEventListener,而不是keyup。更改它应解决您的问题。

答案 1 :(得分:0)

<强>已更新

查看此运行演示。我希望它能满足您的需求。

&#13;
&#13;
// store element reference in variable, e.g. if list has this id
var items = document.querySelectorAll('#listMemo li');

// assign event listener to each
console.dir(items);
var i;
for (i = 0; i < items.length; i++) {
  items[i].addEventListener('click', removeListItem);
  console.log('add EL to ' + i);
}

function removeListItem(mouseEvent) {
  // get element 
  //debugger;
  var element = mouseEvent.target;
  if (window.confirm('Do you want to delete "' + element.textContent + '" ?')) {
    // remove the node
    element.parentNode.removeChild(element);
  };
}
&#13;
<ul id="listMemo">
  <li>first item</li>
  <li>second item</li>
  <li>third item</li>
  <li>fourth item</li>
</ul>
&#13;
&#13;
&#13;