![](/img/trans.png)
[英]How to add active class to nav ul li a element when it has some sub pages with it?
[英]How to add a class to li element which has a ul child element in pure Javascript not jQuery
如何将类添加到具有子<ul>
元素的<li>
<ul>
元素。 我发现 jQuery 中有很多示例,但在 Javascript 中没有。 我想用纯 Javascript 来做性能优化。 代码如下: 提前致谢!
<nav id="navigation">
<li><a href="#">Home</a></li>
<li> <!-- >>> I want to add a class to this <li> element as it has a <ul> child element -->
<a href="#">Services</a>
<ul>
<li><a href="">Graphic Designing</a></li>
<li><a href="">Web Designing</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</nav>
我想你需要这样的东西
// -------- vanilla js
document.querySelectorAll('li').forEach((el) => {
if(el.querySelector('ul')) el.classList.add('theClassNameYouNeed');
});
// -------- jQuery (just to see the difference) :)
$('li').each(function() {
const el = $(this);
if(el.find('ul').length) el.addClass('theClassNameYouNeed');
});
从 jQuery 迁移到纯 Javascript (Vanilla) 的有用资源:
document.querySelectorAll('li').forEach((el) => { if(el.querySelector('ul')) el.classList.add('theClassNameYouNeed'); });
.theClassNameYouNeed { background: green; }
<li><a href="#">Home</a></li> <li> <a href="#">Services</a> <ul> <li><a href="">Graphic Designing</a></li> <li><a href="">Web Designing</a></li> </ul> </li> <li><a href="#">Contact</a></li>
这是将类添加到以ul
为子元素的li
元素的一种可能方式
const liElem = document.querySelectorAll("li") liElem.forEach(elem => { if(elem.querySelector("ul")) { elem.classList.add("new-class"); } })
.new-class { color: red; background: #ececec }
<li><a href="#">Home</a></li> <li> <a href="#">Services</a> <ul> <li><a href="">Graphic Designing</a></li> <li><a href="">Web Designing</a></li> </ul> </li> <li><a href="#">Contact</a></li>
您可以在以下两种方式中仅使用JavaScript
来完成此操作。
这是将类添加到其中包含ul
的li
简单direct
解决方案。 这将选择 DOM 中的第一个li > ul
并使用 classList 和add方法对其应用类。
现场演示:
window.addEventListener('DOMContentLoaded', (event) => { let getLiUL = document.querySelector('li > ul') getLiUL.parentElement.classList.add('foo') });
.foo { background: green; }
<nav> <li><a href="#">Home</a></li> <li>I want to add a class to this element as it has a child element <a href="#">Services</a> <ul> <li><a href="">Graphic Designing</a></li> <li><a href="">Web Designing</a></li> </ul> </li> <li><a href="#">Contact</a></li> </nav>
这是使用querySelectorAll
方法和forEach
循环的解决方案。 这将遍历DOM
中的所有li
,并将类应用于唯一将ul
作为子项的li
。
现场演示:
window.addEventListener('DOMContentLoaded', (event) => { let getLiUL = document.querySelectorAll('li') getLiUL.forEach(function(e) { e.querySelector('ul') ? e.classList.add('foo') : " " }) });
.foo { background: green; }
<nav> <li><a href="#">Home</a></li> <li>I want to add a class to this element as it has a child element <a href="#">Services</a> <ul> <li><a href="">Graphic Designing</a></li> <li><a href="">Web Designing</a></li> </ul> </li> <li><a href="#">Contact</a></li> </nav>
参考:
演示片段
const liElems = document.querySelectorAll('li'); liElems.forEach((elem) => { const childrenElems = elem.querySelectorAll('ul') if(childrenElems.length > 0){ elem.setAttribute("class", "democlass") } });
<html> <head></head> <body> <li><a href="#">Home</a></li> <li> <a href="#">Services</a> <ul> <li><a href="">Graphic Designing</a></li> <li><a href="">Web Designing</a></li> </ul> </li> <li><a href="#">Contact</a></li> <script src="script.js"></script> <body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.