开发者

How to use jQuery hasClass when the selector is a JS variable

I am trying to use jQuery's hasClass function. It doesn't seem to work when I use it like this. I would appreciate it if anyone can work out how to use hasClass in this situation.

The error I am getting is

numValueElement.hasClass is not a function
if($(numValueElement.hasClass("tag"))) 

The 开发者_如何学Ckey line throwing this error is this one if($(numValueElement.hasClass("tag")))

$(".numberValue").click
    (
        function ()
        {
            var numValueElement = this;
            var propertyId = numValueElement.id;
            $(".numberBounds").filter("#"+propertyId).toggle();
            if($(numValueElement.hasClass("tag")))
            {
            }

        }
    );


.hasClass() is a jQuery object method, not something attached to the element itself.

You need to know the difference between a jQuery object wrapped element, and a plain DOM element.

What you want is:

$(numValueElement).hasClass("class-name")


Like this:

if( $(yourVariable).hasClass("someClass") ) {

}


if($(numValueElement).hasClass("tag"))


Use it in this way $(numValueElement).hasClass("tag")


if($('#'+this.id).hasClass('class-name')){

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜