javascript 如何使用纯Js添加和删除同一个类?在“mouseover”事件上添加类,在“mousedown”事件上删除类

62lalag4  于 2023-08-02  发布在  Java
关注(0)|答案(3)|浏览(105)

如何使用纯Js添加和删除同一个类?在“mouseover”事件上添加类,在“mousedown”事件上删除类。
超文本标记语言

<section class="preview">
        <div class="container">
            <div class="preview__wrapper">
                <div class="preview__text">
                    <h1>Navigating the digital landscape for success</h1>
                    <div class="preview__descrp">Our digital marketing agency helps businesses grow and succeed online through a range of services including SEO, PPC, social media marketing, and content creation.</div>
                    <button onmouseover="result(1)" onmousedown="result(1)"  class="btn btn_prev">Book a consultation</button> // event button!
                </div>
            </div>
        </div>
    </section>

字符串
SCSS

.btn{
    width: 231px;
    height: 68px;
    border-radius: 14px;
    border-color: #191A23;
    background-color: #fff;
    color: #000;
    font-size: 20px;
    padding: 0;
    &_prev{
        color: #fff;
        background-color: #191A23;
        width: 264px;
    }
    &_sh{
        box-shadow: 5px 5px darkgoldenrod;
    }
}


JS

let el = document.querySelectorAll('button');

function result(n){
    for (let i = 0; i < el.length; i++){
        el[n].onmouseover = el[n].onmousedown = changeButton;
        function changeButton(event){
            if(event.type =='mouseover'){
                for(let i = 0; i < el.length; i++){
                    if(!el[n].classList.contains('btn_sh')){
                        el[n].classList.add('btn_sh')
                    }
                }
            } 
            if(event.type == 'mousedown'){
                for(let i = 0; i < el.length; i++){
                    if(el[n].classList.contains('btn_sh')){
                        el[n].classList.remove('btn_sh')
                    }
                }   
            }
        }
    }
   
}

k10s72fa

k10s72fa1#

这里不需要JS,只需要使用.btn:hover:not(:active) CSS选择器。
这意味着该样式在鼠标悬停时处于活动状态,但在单击按钮时不处于活动状态。

.btn{
    width: 231px;
    height: 68px;
    border-radius: 14px;
    border-color: #191A23;
    background-color: #fff;
    color: #000;
    font-size: 20px;
    padding: 0;
}
.btn_prev{
   color: #fff;
    background-color: #191A23;
    width: 264px
}
.btn:hover:not(:active){
    box-shadow: 5px 5px darkgoldenrod;
}

个字符

qlvxas9a

qlvxas9a2#

我对你的代码做了一些修改,请试着这样做
js文件

let el = document.querySelectorAll('button');

function result(n, eventType){
    for (let i = 0; i < el.length; i++) {
      if(eventType === 'mouseover'){
          if(!el[n].classList.contains('btn_sh')){
             el[n].classList.add('btn_sh')
           }
      } 
      if(eventType === 'mousedown'){
          if(el[n].classList.contains('btn_sh')){
             el[n].classList.remove('btn_sh')
          }  
      }
    }
}

字符串
Html文件

<section class="preview">
        <div class="container">
            <div class="preview__wrapper">
                <div class="preview__text">
                    <h1>Navigating the digital landscape for success</h1>
                    <div class="preview__descrp">Our digital marketing agency helps businesses grow and succeed online through a range of services including SEO, PPC, social media marketing, and content creation.</div>
                    <button onmouseover="result(0, 'mouseover')" onmousedown="result(0, 'mousedown')"  class="btn btn_prev">Book a consultation</button> // event button!
                </div>
            </div>
        </div>
    </section>

pbwdgjma

pbwdgjma3#

使用此代码,您可以添加任意多个按钮,并且每个按钮都将应用hover和mouseout事件,允许您在鼠标与它们交互时自定义它们的外观。
html -代码不变(在“按钮”标签中,我删除了onmouseover和onmouseout)

let btn = document.querySelectorAll('button');

function toggleClassOnHover(event){
    const eventType = event.type;
    const n = Array.from(btn).indexOf(event.target);

    if(eventType === 'mouseover'){
        btn.classList.add('btn_sh');
    }

    if(eventType === 'mouseout'){
        btn.classList.remove('btn_sh');
    }
}

btn.forEach((button) => {
    button.addEventListener('mouseover', toggleClassOnHover);
    button.addEventListener('mouseout', toggleClassOnHover);
})

字符串

相关问题