hasClass doesn't work in my js code?

前端 未结 2 1227
梦毁少年i
梦毁少年i 2021-01-27 23:28

I want to use hasClass in the following code, but that doesn\'t work for me, please see my demo in jsfiddle and tell me, what do I do wrong?



        
2条回答
  •  离开以前
    2021-01-28 00:02

    It is not the element that exposes the IdRelation class, but its child. Try:

    if ($("span input").hasClass("IdRelation")) {
        alert("ok");
    }​
    

    Or maybe, depending on your actual markup:

    if ($("span > input").hasClass("IdRelation")) {
        alert("ok");
    }​
    

    The selector in the first code snippet will match all elements that are descendants of a element, whereas the selector in the second snippet will match all elements that are direct children of a element. Both will match the element in your sample markup.

提交回复
热议问题