Jquery: Checking to see if div contains text, then action

后端 未结 7 1308
野趣味
野趣味 2020-11-28 06:47

I\'m trying to check in jQuery if a div contains some text, and then add a class if it does.

So I wrote something like this:

    if( $(\"#field >          


        
7条回答
  •  醉话见心
    2020-11-28 07:17

    You might want to try the contains selector:

    if ($("#field > div.field-item:contains('someText')").length) {
        $("#somediv").addClass("thisClass");
    }
    

    Also, as other mentioned, you must use == or === rather than =.

提交回复
热议问题