【问题标题】:Hide a class from other class with the same name when clicked单击时隐藏具有相同名称的其他类的类
【发布时间】:2018-05-08 22:20:45
【问题描述】:

一张图片显示在我的网页上,所有图片都具有相同的类名。 我尝试通过点击隐藏一个特定的类,但是每当我点击该特定类时,所有类都会受到影响

我使用 PHP 将图像回显到网页。我已经尽我所能但没有工作。

<a href="#"><img class="media_image" onclick="hide()" src="media/<?php echo $media?>"/></a>


<script>
function hide(){
var divs = document.getElementsByClassName("media_image");
for(var i = 0; i <= divs.length; i++){
console.log("Item: ", i);
}
}
</script>

【问题讨论】:

  • 请改进(英文)语法。很难说会问什么。

标签: javascript php jquery html css


【解决方案1】:

您可以将onclick="hide()"更改为onclick="hide(this)"将元素传递给函数,然后可以将函数更改为接受它。

function hide (element) {
    //do whatever with element
    element.style.display = "none";
}

【讨论】:

    【解决方案2】:

    您可以为网页中的每个图像添加第二个特殊类,例如:

    <img class="myimg img-1">
    <img class="myimg img-2">
    <img class="myimg img-3">
    // ect
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2019-11-23
      相关资源
      最近更新 更多