我希望能够单击列表项,然后按退格键将其删除。我将如何使用 jQuery 来做到这一点?
$('<li>Click Me</li>')
.appendTo('#list')
.click(function(){
$(this).addClass('delete');
$(this).focus(); // doesn't seem to do anything maybe??
})
.keypress(function(e){
// this event handler doesn't fire
var key = (e.keyCode ? e.keyCode : e.which);
if (key === 8) {
if ($(this).hasClass('delete'))
$(this).remove();
}
});
这是我的jsfiddle:
看起来我无法将按键事件附加到列表项。