addClass causing undefined is not a function

坚强是说给别人听的谎言 提交于 2019-12-31 04:17:30

问题


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');
                                }
                            })
                        },


来源:https://stackoverflow.com/questions/29733951/addclass-causing-undefined-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!