【问题标题】:scan values of certain class elements on page load and run a function to several elements with certain values only在页面加载时扫描某些类元素的值并仅对具有某些值的几个元素运行函数
【发布时间】:2021-08-26 23:25:10
【问题描述】:

我正在尝试在页面加载时创建图像审查功能。

  1. 页面加载时扫描某些类元素的值
  2. 如果多个元素的值超过 5
  3. 将 img src 更改为大于 5 的值计数

我想我在阅读 stackoverflow 中的其他问题后知道如何扫描和更改,但我不能只更改特定问题。

据我了解,下面的代码会在扫描单个 6 时审查所有图像

例如: HTML

<div class="auto-listed-box">
<img class="showing-image" src="normal_image.jpg"><p class="report-count">0</p>
</div>

<div class="auto-listed-box">
<img class="showing-image" src="naked_image.jpg"><p class="report-count">6</p>
</div>

<div class="auto-listed-box">
<img class="showing-image" src="normal_image.jpg"><p class="report-count">0</p>
</div>

javascript

$(document).ready(function () {
   var count = $('.report-count').val();
   if (count > 5 ) { 
     $('.showing-image').attr('src', 'censored.jgp');
   }
});

请帮忙

【问题讨论】:

  • 如果你坚持使用 jQuery,你可以使用 .each 循环。
  • 感谢您的建议,我的知识很短,尝试了 vanowm 的答案,问题得到了解决。我也会研究这个 .each 循环!

标签: javascript html jquery image if-statement


【解决方案1】:

您可以为此使用filter()

$(document).ready(function () {
  $('.report-count')
    .filter((i, node) => +node.textContent > 5)
    .prev()
    .attr("src", 'censored.jpg');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="auto-listed-box">
<img class="showing-image" src="normal_image.jpg"><p class="report-count">0</p>
</div>

<div class="auto-listed-box">
<img class="showing-image" src="naked_image.jpg"><p class="report-count">6</p>
</div>

<div class="auto-listed-box">
<img class="showing-image" src="normal_image.jpg"><p class="report-count">0</p>
</div>

同样的事情,但使用 vanilla javascript(又名更快):

document.addEventListener("DOMContentLoaded", e =>
{
  const report = document.querySelectorAll(".report-count");
  for(let i = 0; i < report.length; i++)
  {
    if (+report[i].textContent > 5)
    {
      report[i].previousSibling.src = 'censored.jpg';
 /* use this line instead if img and p.report-count are not next to each other, but they have common parent */
      //report[i].parentNode.querySelector("img.showing-image").src = 'censored.jgp';
    }
  }
});
<div class="auto-listed-box">
<img class="showing-image" src="normal_image.jpg"><p class="report-count">0</p>
</div>

<div class="auto-listed-box">
<img class="showing-image" src="naked_image.jpg"><p class="report-count">6</p>
</div>

<div class="auto-listed-box">
<img class="showing-image" src="normal_image.jpg"><p class="report-count">0</p>
</div>

如果 img.report-count 不是兄弟姐妹:

document.addEventListener("DOMContentLoaded", () =>
{
  const box = document.querySelectorAll(".auto-listed-box");
  for(let i = 0; i < box.length; i++)
  {
    if (+box[i].querySelector(".report-count").textContent > 5)
    {
      box[i].querySelector("img.showing-image").src = 'censored.jpg';
    }
  }
});
<div class="auto-listed-box">
<img class="showing-image" src="normal_image.jpg"><p class="report-count">0</p>
</div>

<div class="auto-listed-box">
<img class="showing-image" src="naked_image.jpg"><p class="report-count">6</p>
</div>

<div class="auto-listed-box">
<img class="showing-image" src="normal_image.jpg"><p class="report-count">0</p>
</div>

【讨论】:

  • 这多个&lt;p&gt;标签是否属于同一个类?如果没有,那么这应该可以正常工作。但是,如果.report-count 元素不在图像旁边,您将需要使用类似这样的东西(假设pimg 元素都有共同的父元素):report[i].parentNode.querySelector("img.showing-image").src = 'censored.jgp';(问题消失了.. .)
  • 很抱歉删除评论问题,我想在多问之前多尝试,但你太快了。已删除的问题:谢谢,但我有多个 p 标签,如何更改代码?
  • img 标签和 p 标签是相距甚远的后代和祖先,有没有什么办法可以在不使他们成为父母和孩子的情况下完成这项工作?
  • 我使用 Pycharm 并且错误控制台说“e”是未使用的参数,这可以忽略吗?
  • 我添加了第三个变体。 “e”错误可以忽略。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 2017-11-14
  • 1970-01-01
  • 2015-12-27
  • 1970-01-01
相关资源
最近更新 更多