简体   繁体   English

类更改后,单击事件处理程序不起作用

[英]click event handler not working after class changed

If the user clicks on one of the ul s then the color and the class should be changing. 如果用户单击ul之一,则颜色和类别应在更改。 I solved it with this code, but I noticed that the event handlers are not working anymore after I change the class. 我用这段代码解决了它,但是我注意到在更改类后事件处理程序不再起作用。

The ul s should change their color on each click. 每次单击时, ul都应更改其颜色。

 $("ul.AAA").click(function() { $(this).css("background-color", "yellow"); $(this).removeClass("AAA"); $(this).addClass("BBB"); }) $("ul.BBB").click(function() { $(this).css("background-color", "blue"); $(this).removeClass("BBB"); $(this).addClass("AAA"); }) 
 div#start { border: 1px solid black; cursor: pointer; } ul.AAA { background-color: red; } ul.BBB { background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="AAA"> <li> <p>LIST 1</p> </li> <li> <div> <div>A</div> <div>B</div> <div>C</div> </div> </li> </ul> <ul class="BBB"> <li> <p>LIST 2</p> </li> <li> <div> <div>A</div> <div>B</div> <div>C</div> </div> </li> </ul> 


I also tried it with on : 我也尝试了on

 $("ul.AAA").on("click", function() { $(this).css("background-color", "yellow"); $(this).removeClass("AAA"); $(this).addClass("BBB"); }) $("ul.BBB").on("click", function() { $(this).css("background-color", "blue"); $(this).removeClass("BBB"); $(this).addClass("AAA"); }) 
 div#start { border: 1px solid black; cursor: pointer; } ul.AAA { background-color: red; } ul.BBB { background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="AAA"> <li> <p>LIST 1</p> </li> <li> <div> <div>A</div> <div>B</div> <div>C</div> </div> </li> </ul> <ul class="BBB"> <li> <p>LIST 2</p> </li> <li> <div> <div>A</div> <div>B</div> <div>C</div> </div> </li> </ul> 

How can I solve this problem? 我怎么解决这个问题?

As you're changing classes dynamically you need to use on() , however you need to use the delegated version of it. 在动态更改类时,需要使用on() ,但是需要使用它的委托版本。 Try this: 尝试这个:

 $(document).on("click", "ul.AAA", function() { $(this).css("background-color", "yellow"); $(this).toggleClass("AAA BBB"); }) $(document).on("click", "ul.BBB", function() { $(this).css("background-color", "blue"); $(this).toggleClass("AAA BBB"); }) 
 div#start { border: 1px solid black; cursor: pointer; } ul.AAA { background-color: red; } ul.BBB { background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="AAA"> <li> <p>LIST 1</p> </li> <li> <div> <div>A</div> <div>B</div> <div>C</div> </div> </li> </ul> <ul class="BBB"> <li> <p>LIST 2</p> </li> <li> <div> <div>A</div> <div>B</div> <div>C</div> </div> </li> </ul> 

Also note the use of toggleClass() over addClass() and then removeClass() 另请注意,在addClass()之后使用toggleClass() addClass() ,然后在removeClass()

Currently what you are using is called a "direct" binding which will only attach to element that exist on the page at the time your code makes the event binding call. 当前,您所使用的被称为“直接”绑定,它将仅附加到您的代码进行事件绑定调用时页面上存在的元素。

You need to use Event Delegation using .on() delegated-events approach, when manipulation selector (like removing and adding classes). 在操纵选择器(例如删除和添加类)时,需要使用.on()委托事件方法使用事件委托。

General Syntax 一般语法

$(document).on('event','selector',callback_function)

Example

$(document).on("click", "ul.AAA", function() {
    //Rest of your code
});

In place of document you should use closest static container. 代替document您应该使用最近的静态容器。

The delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. 委托事件的优点是,它们可以处理以后在后代添加到文档中的后代元素中的事件。 By picking an element that is guaranteed to be present at the time the delegated event handler is attached, we can use delegated events to bind the click event to dynamically created elements and also to avoid the need to frequently attach and remove event handlers. 通过选择在附加委托事件处理程序时保证存在的元素,我们可以使用委托事件将click事件绑定到动态创建的元素,并且还避免了频繁附加和删除事件处理程序的需要。

A good read Direct and delegated events 良好阅读直接事件和委托事件

 $(document).on("click","ul.AAA",function() {//delegate the event to document //$(this).css("background-color", "yellow"); $(this).removeClass("AAA"); $(this).addClass("BBB"); } ) $(document).on("click","ul.BBB",function() {//delegate the event to document //$(this).css("background-color", "blue"); $(this).removeClass("BBB"); $(this).addClass("AAA"); } ) 
 div#start { border: 1px solid black; cursor: pointer; } ul.AAA { background-color: red; } ul.BBB { background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="AAA"> <li> <p>LIST 1</p> </li> <li> <div> <div>A</div> <div>B</div> <div>C</div> </div> </li> </ul> <ul class="BBB"> <li> <p>LIST 2</p> </li> <li> <div> <div>A</div> <div>B</div> <div>C</div> </div> </li> </ul> 

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

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