我怎样才能提高使用 Javascript 定位我的 html 元素的效率

我有一个带有链接的菜单,当我单击每个链接时,我让它们将小时的不透明度切换为 1,当然,使菜单上的所有其他链接(小时)恢复到他们之前的原始不透明度 0(经典菜单 UX ).


问题是要实现这一点,我必须使用 2 个嵌套的 for 循环使用 JS。从我对 Big O 表示法的了解来看,这不是很有效,尤其是使用 jquery 时,所有这些代码都在一行中完成。我的问题是如何仅使用 Vanilla JS 提高此代码的效率?


HTML


<div className="menu">

    <h6>Home<hr/></h6>

    <h6>Movies<hr/></h6>

    <h6>TV Shows<hr/></h6>

    <h6>Documentaries<hr/></h6>

    <h6>Favorites<hr/></h6>

    <h6>Collection<hr/></h6>

</div>

JS


const menulink = document.querySelectorAll('.menu h6')

for(let item of menulink) {

  item.onclick = () => {

    for(let i=0;i<menulink.length;i++) {

      menulink[i].querySelector('hr').style.opacity = '0'     

    }

    item.querySelector('hr').style.opacity = '1'

  } 


炎炎设计
浏览 103回答 5
5回答

牧羊人nacy

我认为你可以实现这个保存当前项目:const menulink = document.querySelectorAll('.menu h6');let current = null;for(let item of menulink) {&nbsp; item.onclick = () => {&nbsp; &nbsp; if (current) current.style.opacity = '0'&nbsp; &nbsp; current = item.querySelector('hr')&nbsp; &nbsp; current.style.opacity = '1'&nbsp; }&nbsp;}hr {&nbsp; opacity: 0;}<div class="menu">&nbsp; &nbsp; <h6>Home<hr/></h6>&nbsp; &nbsp; <h6>Movies<hr/></h6>&nbsp; &nbsp; <h6>TV Shows<hr/></h6>&nbsp; &nbsp; <h6>Documentaries<hr/></h6>&nbsp; &nbsp; <h6>Favorites<hr/></h6>&nbsp; &nbsp; <h6>Collection<hr/></h6></div>

梦里花落0921

看哪!const menulink = document.querySelectorAll('.menu h6')let activeHr;for(let item of menulink) {&nbsp; item.onclick = (event) => {&nbsp; &nbsp; if (activeHr) {&nbsp; &nbsp; &nbsp; &nbsp; activeHr.style.opacity = '0';&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; activeHr = event.currentTarget.querySelector('hr');&nbsp; &nbsp; activeHr.style.opacity = '1';&nbsp; }&nbsp;}&nbsp;hr {opacity: 0;}<div class="menu">&nbsp; &nbsp; <h6>Home<hr/></h6>&nbsp; &nbsp; <h6>Movies<hr/></h6>&nbsp; &nbsp; <h6>TV Shows<hr/></h6>&nbsp; &nbsp; <h6>Documentaries<hr/></h6>&nbsp; &nbsp; <h6>Favorites<hr/></h6>&nbsp; &nbsp; <h6>Collection<hr/></h6></div>

www说

以上所有答案都试图使您的代码正常工作-而不是考虑最好的 html 和 CSS-不需要使用 hr-您只需要在元素上设置活动类并让 CSS 应用边框底部给它。这样标题就不会在点击时跳转——我在每个 h6 下添加了一个透明边框,然后当你点击它时应用活动类,样式只是为底部边框着色。您不应出于样式目的使用 html 元素 (hr/) - IMO。我也不同意在这里使用 h6——这些 o 似乎不是标题……但我把它留了下来,以防代码比你显示的更多——例如,如果它们是页面下方的标题——但是常规导航列表在这里似乎更合适。感谢@Barmar 提供了我使用的代码框架,我同意他使用活动类的方法。请注意,我正在使用空检查来删除现有的活动类 - 尽管可以通过简单地将第一个标题从一开始就设置为活动来缓解这种情况。const menuLinks = document.querySelectorAll('.menu h6')for (let menuLink of menuLinks) {&nbsp; menuLink.onclick = () => {&nbsp; &nbsp; document.querySelector('.menu h6.active')?.classList.remove('active');&nbsp; &nbsp; menuLink.classList.add('active');&nbsp; }}.menu h6 {&nbsp; padding-bottom: 2px;&nbsp; border-bottom: solid 1px transparent;&nbsp; transition: all 0.2s ease-in-out}.menu h6.active {&nbsp; border-bottom-color: #000}.menu h6:hover {&nbsp; border-bottom-color: #000;&nbsp; transition: all 0.25s ease-in-out}<div class="menu">&nbsp; <h6>Home</h6>&nbsp; <h6>Movies</h6>&nbsp; <h6>TV Shows</h6>&nbsp; <h6>Documentaries</h6>&nbsp; <h6>Favorites</h6>&nbsp; <h6>Collection</h6></div>

偶然的你

您可以在设置标题标签样式时使用悬停选项。在悬停中,您可以添加您选择的颜色,这样每当您将鼠标悬停在它上面时,颜色就会改变

九州编程

不要直接设置样式,通过 CSS 类的样式来设置。然后你可以找到当前有类的元素并删除它,同时将类添加到新选择的元素。const menulink = document.querySelectorAll('.menu h6')for (let item of menulink) {&nbsp; item.onclick = () => {&nbsp; &nbsp; let old = document.querySelector('.menu h6 hr.active');&nbsp; &nbsp; if (old) {&nbsp; &nbsp; &nbsp; old.classList.remove("active");&nbsp; &nbsp; }&nbsp; &nbsp; item.querySelector('hr').classList.add("active");&nbsp; }}.menu h6 hr.active {&nbsp; opacity: 1;}.menu h6 hr {&nbsp; opacity: 0;}<div class="menu">&nbsp; <h6>Home&nbsp; &nbsp; <hr/>&nbsp; </h6>&nbsp; <h6>Movies&nbsp; &nbsp; <hr/>&nbsp; </h6>&nbsp; <h6>TV Shows&nbsp; &nbsp; <hr/>&nbsp; </h6>&nbsp; <h6>Documentaries&nbsp; &nbsp; <hr/>&nbsp; </h6>&nbsp; <h6>Favorites&nbsp; &nbsp; <hr/>&nbsp; </h6>&nbsp; <h6>Collection&nbsp; &nbsp; <hr/>&nbsp; </h6></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript