英文:
How to apply CSS to an element which doesn't have a certain element as a child? JS solution allowed
问题
能否将CSS应用于没有特定元素作为子元素的元素?
我不能使用:has()
,因为我需要支持旧版本的浏览器。允许使用JavaScript解决方案。
请参考以下代码示例。
<!-- 我想要隐藏的元素 -->
<div>
<a class="link"> 点我 </a>
</div>
<!-- 具有<p>的元素。我不想隐藏它。 -->
<div>
<a class="link">
<p class="p-Inside-Link">点我</p>
</a>
</div>
我尝试过但没有成功的方法:
div a:not( > .p-Inside-Link){
display: none;
}
英文:
Is it possible to apply CSS to an element that doesn't have a specific element as a child?
I can't use :has()
because I need to support older browsers. A JavaScript solution is allowed.
See code example.
<!-- Element I want to hide -->
<div>
<a class="link"> click me </a>
</div>
<!-- Element with <p> inside. I don't want to hide it. -->
<div>
<a class="link">
<p class="p-Inside-Link">click me</p>
</a>
</div>
My attempt that did not work:
div a:not( > .p-Inside-Link){
display: none;
}
答案1
得分: 2
不使用:has()
是不可能的。所以在这种情况下,你可以依赖于使用JavaScript
或修改HTML
。
但是,由于在你的情况下无法编辑HTML结构,让我们使用JavaScript
。
// 选择所有包含类名为.link的元素的div
const links = document.querySelectorAll('div .link');
// 过滤链接,只保留不包含类名为.p-Inside-Link的div
const linksToHide = [...links].filter(link => !link.querySelector('.p-Inside-Link'));
[...linksToHide].forEach(link => {
// 使用link元素的parentNode隐藏周围的div元素
link.parentNode.style.display = 'none';
});
<!-- 要隐藏的元素 -->
<div>
<a class="link">隐藏我</a>
</div>
<!-- 包含<p>的元素,我不想隐藏它 -->
<div>
<a class="link">
<p class="p-Inside-Link">显示我</p>
</a>
</div>
英文:
Without using :has()
this is not possible. So in this case you rely on using JavaScript
or alter the HTML
.
But since editing the HTML structure is not possible in your case lets use JavaScript
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Select all divs that contain an element with class .link
const links = document.querySelectorAll('div .link');
// Filter the links to only divs that don't contain
// an class with .p-Inside-Link
const linksToHide = [...links].filter(link => !link.querySelector('.p-Inside-Link'));
[...linksToHide].forEach(link => {
// Use parentNode on the link element to hide the surounding div element
link.parentNode.style.display = 'none';
});
<!-- language: lang-html -->
<!-- Element I want to hide -->
<div>
<a class="link"> HIDE ME </a>
</div>
<!-- Element with <p> inside. I don't want to hide it. -->
<div>
<a class="link">
<p class="p-Inside-Link">SHOW ME</p>
</a>
</div>
<!-- end snippet -->
答案2
得分: 2
这是一个简单的解决方案。循环遍历一个 div 内部的 .link 锚点。然后对于每个链接,检查是否存在 p-inside-link。如果不存在,则添加一个名为 hide 的 CSS 类。然后在 CSS 中添加一个具有 display:none 的 hide 类。
通过添加类而不是通过 JavaScript 修改样式,可以为您提供更多未来的功能。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const links = document.querySelectorAll("div .link");
links.forEach((link) => {
if(!link.querySelector(".p-Inside-Link")){
link.parentNode.classList.add("hide");
}
});
<!-- language: lang-css -->
.hide{display:none;}
<!-- language: lang-html -->
<!-- Element I want to hide -->
<div>
<a class="link"> click me </a>
</div>
<!-- Element with <p> inside. I don't want to hide it. -->
<div>
<a class="link">
<p class="p-Inside-Link">click me (keep)</p>
</a>
</div>
<!-- end snippet -->
英文:
Here is a simple solution. Loop through the .link anchors inside of a div. Then for each link check to see if p-inside-link doesn't exist. If it doesn't exist add a CSS class called hide. Then in CSS add a class for hide that has a display of none.
Adding classes instead of modifying the style via javascript will give you more future capabilities.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const links = document.querySelectorAll("div .link");
links.forEach((link) => {
if(!link.querySelector(".p-Inside-Link")){
link.parentNode.classList.add("hide");
}
});
<!-- language: lang-css -->
.hide{display:none;}
<!-- language: lang-html -->
<!-- Element I want to hide -->
<div>
<a class="link"> click me </a>
</div>
<!-- Element with <p> inside. I don't want to hide it. -->
<div>
<a class="link">
<p class="p-Inside-Link">click me (keep)</p>
</a>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论