gpt4 book ai didi

javascript - 将样式应用于父 div

转载 作者:太空宇宙 更新时间:2023-11-03 20:06:02 25 4
gpt4 key购买 nike

我有以下 html:

<div class="um-field field-date">
<p class="form-row " id="date_field">
<label class="date">
<input data-label="Date" data-value="" type="date" class="input-date um-frontend-field um-hide-field" name="date_1521080645" id="date_1521080645"> Date
</label>
</p>
</div>

有一个um-hide-field 类。我需要隐藏应用于该类的整个 div。这可能吗?

.um-hide-field {
display:none;
}

隐藏整个div。就像靠近父 div。

最佳答案

你可以使用 javascript 来获取这个...尝试使用 parentNode javascript

var hide = document.querySelectorAll(".um-hide-field");
Array.from(hide).forEach(function(elem) {
var div = elem.parentNode.parentNode.parentNode;
div.style.display = "none";
})
<div class="um-field field-date">
<p class="form-row " id="date_field1">
<label class="date"><input data-label="Date" data-value="" type="date" class="input-date um-frontend-field um-hide-field" name="date_1521080645" id="date_1521080645">Date1</label>
</p>
</div>
<div class="um-field field-date">
<p class="form-row " id="date_field2">
<label class="date"><input data-label="Date" data-value="" type="date" class="input-date um-frontend-field" name="date_1521080646" id="date_1521080645">Date2</label>
</p>
</div>
<div class="um-field field-date">
<p class="form-row " id="date_field3">
<label class="date"><input data-label="Date" data-value="" type="date" class="input-date um-frontend-field um-hide-field" name="date_1521080645" id="date_1521080647">Date3</label>
</p>
</div>


或者使用 closest() jQuery

$("input.um-hide-field").closest(".um-field").hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="um-field field-date">
<p class="form-row " id="date_field1">
<label class="date"><input data-label="Date" data-value="" type="date" class="input-date um-frontend-field um-hide-field" name="date_1521080645" id="date_1521080645">Date1</label>
</p>
</div>
<div class="um-field field-date">
<p class="form-row " id="date_field2">
<label class="date"><input data-label="Date" data-value="" type="date" class="input-date um-frontend-field" name="date_1521080646" id="date_1521080645">Date2</label>
</p>
</div>
<div class="um-field field-date">
<p class="form-row " id="date_field3">
<label class="date"><input data-label="Date" data-value="" type="date" class="input-date um-frontend-field um-hide-field" name="date_1521080645" id="date_1521080647">Date3</label>
</p>
</div>

关于javascript - 将样式应用于父 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49461521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com