Javascript 单击时禁用/启用mouseenter和mouseleave-jquery
我试过这个:Javascript 单击时禁用/启用mouseenter和mouseleave-jquery,javascript,function,toggle,jquery,Javascript,Function,Toggle,Jquery,我试过这个: $('.aaa').mouseenter(function () { $(this).css('background', '#aaaaaa'); $(this).css('border', 'solid 1px red'); }); $('.aaa').mouseleave(function () { $(this).css('background','blue'); }); $('#tog').click(function () { $('.a
$('.aaa').mouseenter(function () {
$(this).css('background', '#aaaaaa');
$(this).css('border', 'solid 1px red');
});
$('.aaa').mouseleave(function () {
$(this).css('background','blue');
});
$('#tog').click(function () {
$('.aaa').off('mouseenter mouseleave').on('mouseenter mouseleave');
});
而且它不工作——只是关闭事件/功能
另外,如何只切换函数的一部分-例如$this.css'background','aaaaaa';-而不是切换整个功能
编辑:和。由Shimon Rachlenko解决。您可以使用一些共享变量来标记何时关闭功能:
var flag = true;
$('.aaa').mouseenter(function () {
if(flag) { // enable this functionality only when flag is set
$(this).css('background', '#aaaaaa');
}
$(this).css('border', 'solid 1px red');
});
$('.aaa').mouseleave(function () {
if(!flag) return;
$(this).css('background','blue');
});
$('#tog').click(function () {
flag = !flag;
});
您可以使用某些共享变量来标记何时关闭功能:
var flag = true;
$('.aaa').mouseenter(function () {
if(flag) { // enable this functionality only when flag is set
$(this).css('background', '#aaaaaa');
}
$(this).css('border', 'solid 1px red');
});
$('.aaa').mouseleave(function () {
if(!flag) return;
$(this).css('background','blue');
});
$('#tog').click(function () {
flag = !flag;
});
差不多
function mouseenterbk() {
$(this).css('border', 'solid 1px red');
}
function mouseenter() {
$(this).css('background', '#aaaaaa');
}
function mouseleave() {
$(this).css('background', 'blue');
}
$('.aaa').on('mouseenter.bk', mouseenterbk).mouseenter(mouseenter).mouseleave(mouseleave);
//this is a very dump implementation of the click toggle
var flag;
$('#tog').click(function () {
if (flag) {
$('.aaa').on('mouseenter.bk', mouseenterbk);
flag = false;
} else {
$('.aaa').off('mouseenter.bk');
flag = true;
}
});
演示:类似
function mouseenterbk() {
$(this).css('border', 'solid 1px red');
}
function mouseenter() {
$(this).css('background', '#aaaaaa');
}
function mouseleave() {
$(this).css('background', 'blue');
}
$('.aaa').on('mouseenter.bk', mouseenterbk).mouseenter(mouseenter).mouseleave(mouseleave);
//this is a very dump implementation of the click toggle
var flag;
$('#tog').click(function () {
if (flag) {
$('.aaa').on('mouseenter.bk', mouseenterbk);
flag = false;
} else {
$('.aaa').off('mouseenter.bk');
flag = true;
}
});
演示:$'.aaa'。关闭“mouseenter mouseleave”。打开“mouseenter mouseleave”;你期望最后一部分做什么?不清楚你想要达到什么。为了什么?你的最后一行不会做你想的;它将关闭绑定事件处理程序,但on将不执行任何操作,因为您没有指定回调。$'.aaa'。关闭'mouseenter mouseleave'。打开'mouseenter mouseleave';你期望最后一部分做什么?不清楚你想要达到什么。为了什么?你的最后一行不会做你想的;它将关闭绑定事件处理程序,但on将不起任何作用,因为您没有指定回调。谢谢,它可以工作。您能告诉我如何只禁用函数的一部分吗?问题的第二部分。@weaponx答案更新嘿,Shimon,关于您的代码,我还有一个问题,关于如何记住所选的首选项/设置cookie-谢谢,它可以工作。您能告诉我如何只禁用函数的一部分吗?问题的第二部分。@weaponx答案更新嘿,Shimon,关于您的代码,我还有一个问题,关于如何记住所选的首选项/设置cookie-这同样有效。谢谢,这也行。谢谢