jQuery 中的类名更改事件

发布于 2024-09-07 14:52:22 字数 356 浏览 1 评论 0原文

当元素类更改时,有没有办法在 jQuery 中触发事件?

示例:

<img class="selected" />

into

<img class="selected newclass" />

触发事件


And

<img class="selected" />

into

<img class="" />

触发事件

Is there a way to trigger a event in jQuery when an elements classes are changed?

Examples:

<img class="selected" />

into

<img class="selected newclass" />

triggers an event


And

<img class="selected" />

into

<img class="" />

trigger an event

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦情居士 2024-09-14 14:52:22

您也许可以通过一些技巧来解决。 钩子 .addClass() & .removeClass() 像这样:

var _addClass    = $.fn.addClass,
    _removeClass = $.fn.removeClass;

$.fn.addClass    = function(){
    // do whatever here
    return _addClass.apply(this, arguments);
}

$.fn.removeClass = function(){
    // do whatever here
    return _removeClass.apply(this, arguments);
}

这样,假设您只是有兴趣观察元素类的更改jQuery,您可以在添加删除类时触发任何代码。同样,如果您在本例中使用 jQuery。

当然,您可以通过这种方式挂钩覆盖任何 JavaScript 函数。

正如 Nick Craver 在评论中提到的,还有其他几种 jQuery 方法可以添加或删除元素类。您必须hook所有这些,即:

.addClass()
.removeClass()
.toggleClass()
.removeAttr('class')
.attr('class', 'changed')

除非您确切知道调用哪些方法来操作类,否则您必须关心所有这些。

You might be able to workaround on a little hacky way. Hook .addClass() & .removeClass() like so:

var _addClass    = $.fn.addClass,
    _removeClass = $.fn.removeClass;

$.fn.addClass    = function(){
    // do whatever here
    return _addClass.apply(this, arguments);
}

$.fn.removeClass = function(){
    // do whatever here
    return _removeClass.apply(this, arguments);
}

That way, assuming you are just interested in watching elements class changes with jQuery, you can trigger whatever code on adding or removing classes. Again, just if you are using jQuery, for this example.

Of course, you can hook and overwrite any javascript function that way.

As Nick Craver mentioned in a comment, there are several other jQuery methods which can add or remove an elements class. You would have to hook all of those, namely:

.addClass()
.removeClass()
.toggleClass()
.removeAttr('class')
.attr('class', 'changed')

unless you know exactly which methods are called to manipulate a class, you would have to care about all of those.

被翻牌 2024-09-14 14:52:22

我认为这是不可能的。

事件基于用户操作,因此单击、双击、鼠标悬停等都是用户操作。

您可能希望在每个元素上设计某种数据块,然后构建一个集成到 jQuery .class().attr() 方法中的小型系统,以便当您的代码更新一个类,它将向包含前一个类的元素添加数据,

然后如果前一个类与正在设置的类不同,则触发您的事件。

我所说的集成的意思是

var old_ClassFunc = $.fn.class; //Save it

var $.fn.class = function()
{
   if($(this).data('old-class'))
   {
      if($(this).data('class_callback'))#
      {
          $(this).data('class_callback')(this);
      }
   }
   $(this).data('old-class',$(this).attr('class')); //Update Data
   return old_class.apply(this,arguments);
}

你可以这样做

$('element').data('class_callback',function(context){
   alert('Element class has changed');
})

希望这能给你一些帮助

I do not think this is possible.

Events are based on user actions so click,dblclick,mouseover etc are all user actions.

You may want to devise some kind of data block on each element and then build a small system that integrates into jQuery .class() and .attr() methods so that when your code updates a class it will add data to that element holding the previous class,

then if the previous class is different to class being set then trigger your events.

What i mean by integrate is

var old_ClassFunc = $.fn.class; //Save it

var $.fn.class = function()
{
   if($(this).data('old-class'))
   {
      if($(this).data('class_callback'))#
      {
          $(this).data('class_callback')(this);
      }
   }
   $(this).data('old-class',$(this).attr('class')); //Update Data
   return old_class.apply(this,arguments);
}

then you can do like

$('element').data('class_callback',function(context){
   alert('Element class has changed');
})

Hope this gives you some help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文