试图按类匹配并隐藏元素

时间:2017-01-13 01:13:56

标签: jquery

如何根据特定ID隐藏特定的类元素?

例如,在此处的代码中,我想隐藏身份<div class="base">的{​​{1}}

green

2 个答案:

答案 0 :(得分:1)

假设您使用的是jQuery,您可以使用代码$("#green").hide();来完成此任务。要获得“绿色”内的其中一个div。您可以使用$("#green .base").hide();隐藏它。

Hide Method

答案 1 :(得分:0)

// Get all id green element
const allHideElement = document.querySelectorAll("div #green");
// Loop all the element
allHideElement.forEach(function(element){
  // Add the class of "isHdie" of match element,but you can do anything you want :P
  element.classList.add("isHide");
});
.isHide{
  background: skyblue;
  width: 10em;
  height: 10em;
}
<div id="green">
<div class="base" onclick="prirad(1, 'green')"></div>
<div class="three_s" onclick="prirad(2, 'green')"></div>
<div class="three_v" onclick="prirad(3, 'green')"></div>
<div class="two_s" onclick="prirad(4, 'green')"></div>
<div class="two_v" onclick="prirad(5, 'green')"></div>
<div class="square" onclick="prirad(6, 'green')"></div>
</div>
<div id="blue">
<div class="base" onclick="prirad(1, 'blue')"></div>
<div class="three_s" onclick="prirad(2, 'blue')"></div>
<div class="three_v" onclick="prirad(3, 'blue')"></div>
<div class="two_s" onclick="prirad(4, 'blue')"></div>
<div class="two_v" onclick="prirad(5, 'blue')"></div>
<div class="square" onclick="prirad(6, 'blue')"></div>
<div id="green"></div>
</div>