admin管理员组文章数量:1302408
Having html list as ,
<ul id="slider">
<li class=""></li>
<li class=""></li>
<li class=""></li>
<li class="selected"></li>
<li class=""></li>
<li class=""></li>
<li class=""></li>
<ul>
I am adding selected class to any of the li element. Here i have added class="selected" to fourth li element.
Want to execute some code when class selected is added to any of the li element.
tried this one , but not working as expected.
<script>
$(function() {
$('#slider').bind('DOMSubtreeModified', function(e) {
alert("Changed");
});
});
</script>
Having html list as ,
<ul id="slider">
<li class=""></li>
<li class=""></li>
<li class=""></li>
<li class="selected"></li>
<li class=""></li>
<li class=""></li>
<li class=""></li>
<ul>
I am adding selected class to any of the li element. Here i have added class="selected" to fourth li element.
Want to execute some code when class selected is added to any of the li element.
tried this one , but not working as expected.
<script>
$(function() {
$('#slider').bind('DOMSubtreeModified', function(e) {
alert("Changed");
});
});
</script>
Share
Improve this question
edited Jan 22, 2014 at 7:47
dcodesmith
9,6144 gold badges38 silver badges40 bronze badges
asked Jan 13, 2014 at 10:53
Nishant NawarkhedeNishant Nawarkhede
8,40013 gold badges61 silver badges83 bronze badges
2
- 9 When you add class, call specific function... Using any other way would be just bad practice – A. Wolff Commented Jan 13, 2014 at 10:54
- 1 possible duplicate of jQuery - Fire event if CSS class changed – Ram Commented Jan 13, 2014 at 11:00
3 Answers
Reset to default 3Just invoke a function after you add the Class on the click event.
$(function() {
function doSomething() {
.....
}
$('ul').on('click', 'li' function(e) {
$(this).addClass('selected');
doSomething();
});
});
The best way to handle this is just to call your function when you add the class, since it's your code adding it.
There are two mon ways to do that:
You might do that with a direct call:
li.addClass("selected"); doSomethingBecauseItChanged(li);
Or you can do it by raising your own event on the
li
element that other code can listen to, which more thoroughly decouples the code adding the class from the code responding to the change.Here's the code listening for the event:
$("#slider").on("added-class", function() { alert("Changed"); });
And here's the code adding the class and raising the event:
li.addClass("selected").trigger("added-class");
Note that
added-class
is a name I made up, not something that already exists. Use whatever name you like, just make sure not to conflict with existing event types.Here's a plete example: Live Copy | Live Source
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis./ajax/libs/jquery/1.10.1/jquery.min.js"></script> <meta charset=utf-8 /> <title>Event Example</title> <style> li.selected { color: green; } </style> </head> <body> <p>In two seconds, the third item will turn green and you'll see an alert.</p> <ul id="slider"> <li class="">xxxxx</li> <li class="">xxxxx</li> <li class="">xxxxx</li> <li class="">xxxxx</li> <li class="">xxxxx</li> <li class="">xxxxx</li> <li class="">xxxxx</li> </ul> <script> $("#slider").on("added-class", function() { alert("Changed"); }); setTimeout(function() { $("#slider li").eq(2).addClass("selected").trigger("added-class"); }, 2000); </script> </body> </html>
Alternately, if you don't need IE support, you can use a mutation observer (support matrix), but frankly you really shouldn't need to use those if you control the code that's adding the class.
Register handler when you are adding the class.Something like this
$('li').addClass('selected').trigger('class-change');
$('#slider').bind('class-change', function() {
//do something
});
});
本文标签: javascriptDetect event when class is added to liStack Overflow
版权声明:本文标题:javascript - Detect event when class is added to li - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741686092a2392452.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论