简体   繁体   English

在这种情况下如何使用jQuery .off()?

[英]How to use jQuery .off( ) in this situation?

I've been testing this script with many different arguments to put in .off() but can make this work. 我一直在用许多不同的参数测试此脚本,以将其放入.off()中,但可以使此工作正常进行。 I want the boxes to be removable after clicking the "remove" button and I want them to be unremovable after switching the button back to done. 我希望在单击“删除”按钮后可移动这些框,并且在将按钮切换回完成后希望它们不可移动。 This is my code: 这是我的代码:

 var remove = "<button class=\\"extension\\" id=\\"remove\\"><a href=\\"#\\">Remove</a></button>"; var done = "<button class=\\"extension\\" id=\\"done\\"><a href=\\"#\\">Done</a></button>"; $("body").on("click", "#remove", function() { $("#button_field").html(done); $("body").on("click", "button.box", function() { $(this).remove(); }); }); $("body").on("click", "#done", function() { $("#button_field").html(remove); $("body").off(); }); 
  body { font-family: Avenir; } h1, h2, h3, h4, h5, h6 { font-weight: 400; } .center { margin: 0 auto; text-align: center; width: 380px; } .box { background: #F7F7F7; width: 380px; height: 80px; border: 1px solid #D5D5D5; box-shadow: 0 0 5px #BABABA; font-family: inherit; font-size: inherit; } .extension { width: 380px; float: left; margin-top: 10px; height: 50px; background: #F7F7F7; border: 1px solid #D5D5D5; box-shadow: 0 0 5px #BABABA; font-family: inherit; font-size: inherit; } .extension:hover { cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <div class="center"> <h3>Click to delete</h3> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <div id="button_field"> <button class="extension" id="remove"><a href="#">Remove</a> </button> </div> </div> 

Your .off() call is removing ALL the click bindings on body , so you removed the delegation to #remove . 您的.off()调用正在删除body上的所有click绑定,因此您删除了#remove的委托。 You only want to remove the delegations to button.box , so it should be $('body').off('click', 'button.box'); 您只想删除对button.box ,因此应该是$('body').off('click', 'button.box'); .

 var remove = "<button class=\\"extension\\" id=\\"remove\\"><a href=\\"#\\">Remove</a></button>"; var done = "<button class=\\"extension\\" id=\\"done\\"><a href=\\"#\\">Done</a></button>"; $("body").on("click", "#remove", function() { $("#button_field").html(done); $("body").on("click", "button.box", function() { $(this).remove(); }); }); $("body").on("click", "#done", function() { $("#button_field").html(remove); $('body').off('click', 'button.box'); }); 
  body { font-family: Avenir; } h1, h2, h3, h4, h5, h6 { font-weight: 400; } .center { margin: 0 auto; text-align: center; width: 380px; } .box { background: #F7F7F7; width: 380px; height: 80px; border: 1px solid #D5D5D5; box-shadow: 0 0 5px #BABABA; font-family: inherit; font-size: inherit; } .extension { width: 380px; float: left; margin-top: 10px; height: 50px; background: #F7F7F7; border: 1px solid #D5D5D5; box-shadow: 0 0 5px #BABABA; font-family: inherit; font-size: inherit; } .extension:hover { cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <div class="center"> <h3>Click to delete</h3> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <button class="box">REMOVE ME</button> <div id="button_field"> <button class="extension" id="remove"><a href="#">Remove</a> </button> </div> </div> 

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

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