简体   繁体   English

绑定jquery不起作用

[英]bind of jquery does not work

I am experimenting with jQuery. 我正在尝试使用jQuery。 I have a code like 我有一个代码

//some buttons to edit a div's style (add/remove classes)
<button class="size" id="switcher-large"> Large Print </button>
<button class="size" id="switcher-default"> Default </button>
<button class="size" id="switcher-narrow"> Narrow Column </button>

//buttons to activate/deactivate the above buttons
<button class="hide" id="hide"> Hide it </button>
<button class="hide" id="active"> Activate it </button>


//when buttons clicked, add/remove classes to a div
$('#switcher-large').click(function(){$('#one').removeClass().addClass('large'); });    
$('#switcher-default').click(function(){$('#one').removeClass(); });
$('#switcher-narrow').click(function(){$('#one').removeClass().addClass('narrow'); });

//activate/deactivate the buttons with the "size" class
$('#hide').click(function(){ $('.size').unbind('click'); });
$('#active').click(function(){$('.size').bind('click')});

Deactivating the buttons works fine. 取消激活按钮可以正常工作。 Activating them back again does not work. 再次激活它们不起作用。 What am I missing? 我错过了什么?

Thanks in advance. 提前致谢。

$('.size').bind('click') is not valid. $('.size').bind('click')无效。 bind() needs to know which functions to bind as the handler. bind()需要知道要绑定哪些函数作为处理程序。

There are 1,000,000 ways to do this, but you might find it easier to simply disable the buttons. 有1,000,000种方法可以做到这一点,但您可能会发现简单地禁用按钮更容易。 This prevents the click handlers firing; 这可以防止click处理器发射;

$('#hide').click(function(){ $('.size').prop('disabled', true); });
$('#active').click(function(){ $('.size').prop('disabled', false); });

If you really want to persist your use of bind() and unbind() , you need to store a reference to the event handlers you're binding/ unbinding. 如果您真的想继续使用bind()unbind() ,则需要存储对绑定/解除绑定的事件处理程序的引用。 Here's an example for the switcher-large button. 这是switcher-large按钮的示例。

//when buttons clicked, add/remove classes to a div
function switcherLargeHandler(){ $('#one').removeClass().addClass('large'); }

// Bind it by default
$('#switcher-large').click(switcherLargeHandler);  

$('#hide').click(function(){ $('#switcher-large').unbind('click', switcherLargeHandler); });
$('#active').click(function(){$('#switcher-large').bind('click', switcherLargeHandler)});

Note that: 注意:

$('#switcher-large').click(function () { /* Do something */ });

... is exactly the same as: ......与以下内容完全相同:

$('#switcher-large').bind('click', function () { /* Do something */ });

... which is exactly the same as: ......这完全相同:

$('#switcher-large').on('click', function () { /* Do something */ });

FWIW, unbind() without a second parameter will remove all click event handlers bound to an element; 没有第二个参数的FWIW, unbind()将删除绑定到元素的所有 click事件处理程序; which is quite rude if you're in a shared enviroment, as you can potentially remove other event handlers you didn't know about. 如果您在共享环境中,这是非常粗鲁的,因为您可能会删除您不了解的其他事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM