English 中文(简体)
• 如何将现有的班级与贾瓦伦语混为一谈
原标题:How to toggle existing class with JavaScript

我有一把“诱杀装置” i在 anchor子内。

我的守则如下:

<li class="nav-item">
    <a href="#" class="nav-link">
        <i class="bi bi-house"></i>
    </a>
</li>
<li class="nav-item">
    <a href="#" class="nav-link">
        <i class="bi bi-heart"></i>
    </a>
</li>
<li class="nav-item">
    <a href="#" class="nav-link">
        <i class="bi bi-envelope"></i>
    </a>
</li>

如果被点击,我想添加<代码>-填满<>/代码>,并在点击另一条导航时删除。 我曾尝试过<条码>流ist/条码>,但似乎只是增加新类别而没有使用。

<li class="nav-item">
    <a href="#" class="nav-link">
        <i class="bi bi-house-fill"></i>
    </a>
</li>
<li class="nav-item">
    <a href="#" class="nav-link">
        <i class="bi bi-heart"></i>
    </a>
</li>
<li class="nav-item">
    <a href="#" class="nav-link">
        <i class="bi bi-envelope"></i>
    </a>
</li>
问题回答

现行做法的问题在于,如果他们没有到场,就只会增加这一类。 这意味着,只有将原类别与所附“填充”类别相转换,不允许多个要素同时拥有“填充”类别。

在这方面,你如何能够实现理想行为:

1. 取消现有的“填满”类别:

Before adding the "-fill" class to the clicked element, you need to remove it from any other element that might have it. This ensures only one element has the active state at a time.

// Select all anchor elements within the navigation
const navLinks = document.querySelectorAll( .nav-link );

navLinks.forEach(link => {
  // Remove any existing "-fill" class
  link.classList.remove( bi-house-fill ,  bi-heart-fill ,  bi-envelope-fill );
});

2. 在点击元件中添加“填充”类别:

Now, when an anchor element is clicked, you can use classList.toggle to add the "-fill" class specific to the clicked icon.

navLinks.forEach(link => {
  link.addEventListener( click , () => {
    const icon = link.querySelector( i );
    const iconClass = icon.classList[1]; // Get the second class (e.g., "bi-house")
    icon.classList.toggle(`${iconClass}-fill`);
  });
});

<>Explanation:

  1. We use querySelectorAll to select all anchor elements with the class nav-link.
  2. We loop through each link using forEach.
  3. Inside the loop, we use classList.remove to remove any existing "-fill" classes from the icon within the link. This ensures only one element has the active state.
  4. We add an event listener to each link for the click event.
  5. When a link is clicked, we use querySelector to get the icon element within the link.
  6. We then access the second class name of the icon using classList[1]. This assumes the icon class is the second class on the element.
  7. Finally, we use classList.toggle with a template literal to dynamically construct the class name to be toggled. This ensures the appropriate "-fill" class is added or removed based on the clicked icon.

这种办法确保,只有被点击的icon有“填充”类别,为运行中的导航项目提供所需的视觉反馈。

为了实现你们重新寻找的行为,你需要把活动听众添加到你们的主角上。 在点击icon时,如尚未填入,请在舱面上添加<条码>-填满,对于所有其他电码,请删除<条码>-填满<>。 如果在场的话。

For this purpose, you have to assign a click event to each navigation item (or alternatively use event delegation approach) and then use Array#forEach to map through your navlinks, get the <i> classes and replace the -fill with an empty string and finally add the -fill to the clicked element.

const navLinks = document.querySelectorAll( .nav-link );

navLinks.forEach(link => {
  link.addEventListener( click , function() {
    // Remove  -fill  from all icons
    navLinks.forEach(otherLink => {
      const icon = otherLink.querySelector( i );
      icon.className = icon.className.replace( -fill ,   );
    });

    // Add  -fill  to the clicked icon
    const icon = this.querySelector( i );
    if (!icon.className.includes( -fill )) {
      icon.className = icon.className +  -fill ;
    }
  });
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">


<ul class="nav">
  <li class="nav-item">
    <a href="#" class="nav-link">
      <i class="bi bi-house"></i>
    </a>
  </li>
  <li class="nav-item">
    <a href="#" class="nav-link">
      <i class="bi bi-heart"></i>
    </a>
  </li>
  <li class="nav-item">
    <a href="#" class="nav-link">
      <i class="bi bi-envelope"></i>
    </a>
  </li>
</ul>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签