我正在使用以下代码突出显示元素:
$(".ss").live({
mouseenter: function () { HighLight(this) },
mouseleave: function () { OffLight(this); },
keypress: function () { KeyOperation(this); }
});
function HightLight(s)
{
$(s).css({border : "1px solid red"});
}
function OffLight(s)
{
$(s).css({border : "0"});
}
function KeyOperation(s)
{
$(s).remove();
}
KeyOperation()函数未在按键上执行。
在这里我试图做到这一点,当用户突出显示任何元素时,并且突出显示时,如果他按
Delete key
,则应该删除该元素。但这不起作用,有人可以告诉我我该怎么做吗? 最佳答案
我相信这就是你所追求的。将鼠标悬停在任何.ss
元素上将添加一个临时类。如果用户随时按下键46,则将删除该类别的项目。请参阅下面的工作提琴。
// Bind to a closer parent if possible
$(document)
.on("keyup", function(e){
if ( e.which === 46 ) $(".removeMe").remove();
})
.on("mouseenter mouseleave", ".ss", function(f){
$(this).toggleClass("removeMe", f.type === "mouseenter" );
});
从此示例中应该清楚,不再鼓励
$.live
进行事件委派。从这里开始使用$.on
代替。小提琴:http://jsfiddle.net/YS7jH/2/