【问题标题】:If parent div has specific class toggle another div using JS如果父 div 具有特定的类,则使用 JS 切换另一个 div
【发布时间】:2022-01-15 05:21:47
【问题描述】:

如果父 div 使用 JS 有特定的 class,我如何显示 div

在下面的代码中,只有当下面的父 div 具有 class .matched 和孩子divclass .gray

并且,divclass .hidden-purple 仅当 div 下面的父级具有 class .matched 并且子级 div 具有 @987654334 @.紫色

<div class="hidden-gray">
Display this div only if parent div has class .matched AND child div has class .gray
</div>

<div class="hidden-purple">
Display this div only if parent div has class .matched AND child div has class .purple
</div>

<div class="turn matched" id="1">
    <div class="tile gray">
        <div class="face front" style="background-color: transparent;">
            <img src="https://via.placeholder.com/336x221">
        </div>
    </div>
</div>
      
<div class="turn matched" id="2">
    <div class="tile purple">
        <div class="face front" style="background-color: transparent;">
            <img src="https://via.placeholder.com/336x221">
        </div>
    </div>
</div>

CSS

 .mystyle {
 width: 100%;
 padding: 25px;
 background-color: coral;
 color: white;
 font-size: 25px;
 box-sizing: border-box;
 }

.turn {
 margin-top: 10px;
}

.hidden-gray {
 padding: 20px;
 background-color: gray;
 color: white;
 margin-bottom: 10px;
}

.hidden-purple {
 padding: 20px;
 background-color: purple;
 color: white;
 margin-bottom: 10px;
}

非常感谢!

JSFiddle here

【问题讨论】:

  • 嘿实际上我无法理解这个问题。 .hidden-purple? 的“父 div”是哪个
  • 这个 div 有一个子 div 类紫色
    @Musafiroon

标签: javascript html css


【解决方案1】:

这是一种方法。这是我添加的JS代码:

let x = document.getElementById("1");
let y = document.getElementById("2");
let firstChildx = x.children;  
let firstChildy = y.children; 

if (x.className.indexOf('matched') != '-1' && firstChildx[0].className.indexOf('gray')!= '-1') {
let hideg = document.getElementsByClassName("hidden-gray");
hideg[0].style.display = 'block';
}

if (y.className.indexOf('matched') != '-1' && firstChildy[0].className.indexOf('purple')!= '-1' ) {
let hideg = document.getElementsByClassName("hidden-purple");
hideg[0].style.display = 'block';
}

我将.hidden-gray.hidden-purple设置为display:none;,然后使用js代码检查父div的类名和父div的第一个孩子,如果它符合我设置显示的条件返回阻止。

它在这里工作:

let x = document.getElementById("1");
let y = document.getElementById("2");
let firstChildx = x.children;  
let firstChildy = y.children; 

if (x.className.indexOf('matched') != '-1' && firstChildx[0].className.indexOf('gray')!= '-1') {
let hideg = document.getElementsByClassName("hidden-gray");
hideg[0].style.display = 'block';
}

if (y.className.indexOf('matched') != '-1' && firstChildy[0].className.indexOf('purple')!= '-1' ) {
let hideg = document.getElementsByClassName("hidden-purple");
hideg[0].style.display = 'block';
}
.mystyle {
  width: 100%;
  padding: 25px;
  background-color: coral;
  color: white;
  font-size: 25px;
  box-sizing: border-box;
}

.turn {
  margin-top: 10px;
}

.hidden-gray {
  padding: 20px;
  background-color: gray;
  color: white;
  margin-bottom: 10px;
  display: none;
}

.hidden-purple {
  padding: 20px;
  background-color: purple;
  color: white;
  margin-bottom: 10px;
  display: none;
}
<div class="hidden-gray">
Display this div only if parent div has class .matched and child div has class .gray
</div>

<div class="hidden-purple">
Display this div only if parent div has class .matched and child div has class .gray
</div>


<div class="turn matched" id="1">
        <div class="tile gray">
            <div class="face front" style="background-color: transparent;">
                <img src="http://via.placeholder.com/336x221">
            </div>
        </div>
    </div>
  
  <div class="turn" id="2">
        <div class="tile purple">
            <div class="face front" style="background-color: transparent;">
                <img src="http://via.placeholder.com/336x221">
            </div>
        </div>
    </div>

【讨论】:

  • 嗨 John :) 如果我运行代码,紫色和灰色 div 都是可见的,但紫色 div 应该被隐藏,对吗?因为这个 div 不包含类 .matched &lt;div class="turn" id="2"&gt; &lt;div class="tile purple"&gt; &lt;div class="face front" style="background-color: transparent;"&gt; &lt;img src="http://via.placeholder.com/336x221"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;
  • @A-creative 抱歉,我忘了添加部分 if 语句。我已经更新了我的答案。
【解决方案2】:

你可以试试this

首先,添加.hidden 类来隐藏你的hidden-* div。

.hidden {
  display: none;
}

并添加js(检查链接)。

【讨论】:

    猜你喜欢
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 2022-12-03
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多