How to apply CSS to an element which doesn't have a certain element as a child? JS solution allowed

huangapple go评论66阅读模式
英文:

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>

<!-- 具有&lt;p&gt;的元素。我不想隐藏它。 -->
<div>
  <a class="link">
    <p class="p-Inside-Link">点我</p>
  </a>
</div>

我尝试过但没有成功的方法:

div a:not( &gt; .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.

&lt;!-- Element I want to hide --&gt;
&lt;div&gt;
  &lt;a class=&quot;link&quot;&gt; click me &lt;/a&gt;
&lt;/div&gt;

&lt;!-- Element with &lt;p&gt; inside. I don&#39;t want to hide it. --&gt;
&lt;div&gt;
  &lt;a class=&quot;link&quot;&gt;
    &lt;p class=&quot;p-Inside-Link&quot;&gt;click me&lt;/p&gt;
  &lt;/a&gt;
&lt;/div&gt;

My attempt that did not work:

div a:not( &gt; .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(&#39;div .link&#39;);

// Filter the links to only divs that don&#39;t contain 

// an class with .p-Inside-Link
const linksToHide = [...links].filter(link => !link.querySelector('.p-Inside-Link'));

[...linksToHide].forEach(link =&gt; {
  // Use parentNode on the link element to hide the surounding div element
  link.parentNode.style.display = &#39;none&#39;;
});

<!-- language: lang-html -->

&lt;!-- Element I want to hide --&gt;
&lt;div&gt;
  &lt;a class=&quot;link&quot;&gt; HIDE ME &lt;/a&gt;
&lt;/div&gt;

&lt;!-- Element with &lt;p&gt; inside. I don&#39;t want to hide it. --&gt;
&lt;div&gt;
  &lt;a class=&quot;link&quot;&gt;
    &lt;p class=&quot;p-Inside-Link&quot;&gt;SHOW ME&lt;/p&gt;
  &lt;/a&gt;
&lt;/div&gt;

<!-- 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(&quot;div .link&quot;);

links.forEach((link) =&gt; {
  if(!link.querySelector(&quot;.p-Inside-Link&quot;)){
    link.parentNode.classList.add(&quot;hide&quot;);
  }
});

<!-- language: lang-css -->

  .hide{display:none;}

<!-- language: lang-html -->

&lt;!-- Element I want to hide --&gt;
&lt;div&gt;
  &lt;a class=&quot;link&quot;&gt; click me &lt;/a&gt;
&lt;/div&gt;

&lt;!-- Element with &lt;p&gt; inside. I don&#39;t want to hide it. --&gt;
&lt;div&gt;
  &lt;a class=&quot;link&quot;&gt;
    &lt;p class=&quot;p-Inside-Link&quot;&gt;click me (keep)&lt;/p&gt;
  &lt;/a&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月8日 23:00:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定