addClass causing undefined is not a function

2020-04-18 09:40发布

问题:

I have:

mouseOver: function () {
   var catId = this.category_id;
   $('#expenditureSummaryGrid .k-grid-content tr').each(function() {
       if($('td span', this).data('id') == catId) {
           this.addClass('grid-hover');
       }
   })
},

But it's giving me:

Uncaught TypeError: undefined is not a function

Which isn't making much sense since "this" is the expected DOM element I'm trying to add a class too.

What is going wrong?

回答1:

Since .addClass() is a jQuery function, you likely need to change

this.addClass('grid-hover');

to

$(this).addClass('grid-hover');


回答2:

The error of "this" is with the condition inside the for each loop. Can u try

        mouseOver: function () {
                            var catId = this.category_id;
                            $('#expenditureSummaryGrid .k-grid-content tr').each(function(){
                                if(this.find('td span').data('id') == catId) {
                                    this.addClass('grid-hover');
                                }
                            })
                        },