社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

如果元素中没有内容,如何签入jquery?

flash • 7 年前 • 1620 次点击  

我正在一个网站上工作,我想检查元素中是否有任何内容。

下面是我的 html code . 我说过 条件1 哪里 opacity-pointseven 类应该通过脚本添加。

<div class="featured-block">
   <a href="/" class="featured-block__item cf">
      <div class="featured-block__item-inner">
         <figure class="featured-block__image img-fit">   
            <img src="">              // (condition#1)
         </figure>
         <div class="featured-block__content">
            <h1 style="margin-bottom:0px;" class="featured-block__title">Trans Mountain Pipeline: NEB Releases New Report, Recommends Approval </h1>
            <h1 class="featured-block__tag"> More Coverage</h1>
         </div>
      </div>
   </a>
</div>

问题陈述:

我试着用下面的方法,但似乎效果不太好。

<script>
    jQuery(function ($) {
        if ($(this).find(".featured-block__title").not(":empty") && $(this).find(".featured-block__tag").not(":empty")) {
                $(this).find(".img-fit img").addClass("opacity-pointseven");  // For condition#1
            } 

        });
    })
</script>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/41099
文章 [ 2 ]  |  最新文章 7 年前
Florenz
Reply   •   1 楼
Florenz    7 年前

您想检查元素内部是否没有内容或子元素,对吗?

如果这是您的问题,那么您可以使用此代码获取元素的html内容( 此代码的输出是 一串 )

 $(this).find(".featured-block__title").html()

要检查其内容是否为空,可以使用以下条件

 if ($(this).find(".featured-block__title").html().trim() == "") {
    $(this).find(".img-fit img").addClass("default-opacity");
 }

你可以用这个改变你的剧本。

<script>
jQuery(function ($) {
    $(".featured-block__item-inner").each(function () {
        if ($(this).find(".featured-block__title").html().trim() == "" && $(this).find(".featured-block__tag").html().trim() == "") {
            $(this).find(".img-fit img").addClass("default-opacity");
        } else if ($(this).find(".featured-block__title").html().trim() != "" && $(this).find(".featured-block__tag").html().trim() != "") {
            $(this).find(".img-fit img").addClass("opacity-pointseven");
        } else if ($(this).find(".featured-block__title").html().trim() != "" && $(this).find(".featured-block__tag").html().trim() == "") {
            $(this).find(".img-fit img").addClass("opacity-pointseven");
        } else if ($(this).find(".featured-block__title").html().trim() == "" && $(this).find(".featured-block__tag").html().trim() != "") {
            $(this).find(".img-fit img").addClass("opacity-pointseven");
        }

    });
})

Murali Nepalli
Reply   •   2 楼
Murali Nepalli    7 年前

请在javascript中尝试类似的操作,除非必须在jquery中完成。

document.querySelectorAll(".featured-block__item-inner").forEach(function(itemInner){ 

 var blockTitle = itemInner.querySelector(".featured-block__content .featured-block__title");
 var blockTag = itemInner.querySelector(".featured-block__content .featured-block__tag");
 var blockImage = itemInner.querySelector(".featured-block__image, .img-fit");

 if(!(blockTitle && blockTag && blockImage)) return;  

 if(blockTitle.innerText = "" && blockTag.innerText="") {
    blockImage.classList.add("default-opacity");
 }
 else if (blockTitle.innerText != "" && blockTag.innerText !="") {
   blockImage.classList.add("opacity-pointseven");
 }
  .....
})