jQuery 在类已添加类时执行某些操作
我有一个带有“tab3”类的li
。在其他地方有 jQuery,当你将鼠标悬停在它上面时,它会添加“selected”类。当这种情况发生时我想做点什么:
<li class='tab3 selected'>
<div class='showme'>Show this</div>
<div class='addattr'>Add attributes</div>
</li>
$('.tab3.selected').each(function() {
$('.showme').show();
$('.addattr').attr('style', 'position:relative; display:block');
});
I have a li
with class 'tab3'. There is jQuery elsewhere that when you hover on it it adds the class 'selected'. I want to do something when this happens:
<li class='tab3 selected'>
<div class='showme'>Show this</div>
<div class='addattr'>Add attributes</div>
</li>
$('.tab3.selected').each(function() {
$('.showme').show();
$('.addattr').attr('style', 'position:relative; display:block');
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有内置的方法可以做到这一点,但您始终可以为“自定义”事件(例如“类更改”)注册事件处理程序,然后在更新元素的类时触发该事件。 (您甚至可以重新定义“addClass”函数来执行此操作。)
当然,您还应该考虑使用普通的旧 CSS 规则来在添加/删除类时更改外观,如 @Brad Wedell 的答案中所述。
There's no built-in way to do that, but you can always register an event handler for a "custom" event like "class-change", and then trigger that whenever you update an element's class. (You could even redefine the "addClass" function to do that.)
Of course you should also consider using plain old CSS rules to make appearances change when adding/removing classes, as noted in @Brad Wedell's answer.
您可以通过 css 来完成此操作:
You can accomplish this via css: