How to add a class in Javascript/jQuery if an element has content in it?

后端 未结 4 1693
北荒
北荒 2021-01-25 08:56

I am working on a website in which I want to check whether element has any content in it.

Below is my html code. I have mentioned condition#1 where

4条回答
  •  故里飘歌
    2021-01-25 09:25

    You can loop over parent div and find the child items and then add the class over there.

    Here in the example, I am getting $('.featured-block__item-inner') as a parent and then finding items inside it.

    $('.featured-block__item-inner').each(function() {
      if ($(this).find(".featured-block__title").not(":empty").length > 0 && $(this).find(".featured-block__tag").not(":empty").length > 0) {
        $(this).find(".img-fit img").addClass("opacity-pointseven"); // For condition#1
      } else {
        $(this).find(".img-fit img").addClass("default-opacity");
      }
    })
    .opacity-pointseven {
      width: 100px;
      height: 100px;
    }
    
    .default-opacity {
      width: 50px;
      height: 50px;
    }
    
    

提交回复
热议问题