我必须双击“关注”按钮才能运行这是由于使用单击事件2时间

发布于 2025-01-19 21:15:49 字数 808 浏览 6 评论 0原文

我正在研究“关注”按钮,并在 JavaScript 的帮助下编写了以下代码。

但问题是我必须双击“跟随”按钮才能运行,这是由于使用了两次单击事件。我也愿意接受更好的方法来解决这个问题。

 
  var value = null;
    
const onClick = (event) => {
    // event.target.id
 
    value = event.target.id;
  console.log(value);
document.getElementById(`${value}`).addEventListener('click',function(){
    // console.log(value.id);
        if(this.classList.contains('follow')){
        this.classList.remove('follow');
       this.innerHTML ="Following";
       this.style.backgroundColor = 'green' ;
    }else{
        this.classList.add('follow');
        this.style.backgroundColor = 'rgb(27,18,83)' ;
        this.innerHTML="Follow";
    }
    })



}
window.addEventListener('click', onClick);

I am working on follow button and with the help of JavaScript I've come up with the following code.

But the problem is I have to double click the follow button to functioning this is due to using click event 2 time. I am open to better methods of solving this too.

 
  var value = null;
    
const onClick = (event) => {
    // event.target.id
 
    value = event.target.id;
  console.log(value);
document.getElementById(`${value}`).addEventListener('click',function(){
    // console.log(value.id);
        if(this.classList.contains('follow')){
        this.classList.remove('follow');
       this.innerHTML ="Following";
       this.style.backgroundColor = 'green' ;
    }else{
        this.classList.add('follow');
        this.style.backgroundColor = 'rgb(27,18,83)' ;
        this.innerHTML="Follow";
    }
    })



}
window.addEventListener('click', onClick);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┾廆蒐ゝ 2025-01-26 21:15:49

有一个双击事件。您可以检查是否满足您的要求。

mdn链接 - https:https://developer.mozilla.org/en en en -us/doc/web/api/element/dblclick_event

There is a double click event. You can check the if it satisfies your requirement.

MDN link - https://developer.mozilla.org/en-US/docs/Web/API/Element/dblclick_event

烟酉 2025-01-26 21:15:49

对于具有相同功能的多个按钮,我会给它们所有相同的类(例如“btn”)。然后在 JS 中只需获取此类的所有元素,循环遍历您将获取的 HTMLCollection 并为每个元素分配一个事件侦听器。当您想更改按钮上的某些内容时,您必须在函数中使用 event.target

let buttons = document.getElementsByClassName("btn");
for (btn of buttons) {
    btn.addEventListener("click", (event) => {
  
    if(event.target.classList.contains('follow')){
      event.target.classList.remove('follow');
      event.target.innerHTML ="Following";
      event.target.style.backgroundColor = 'green' ;
    }else{
      event.target.classList.add('follow');
      event.target.style.backgroundColor = 'rgb(27,18,83)' ;
      event.target.innerHTML="Follow";
    }
    
  });
}
<button class="btn">1</button>
<button class="btn">2</button>

For multiple buttons with the same function, I would give all of them the same class (e.g. "btn"). Then in JS simply get all of the elements with this class, loop over the HTMLCollection which you would get and assign each element an eventlistener. When you want to change something on the button you have to use event.target in the function:

let buttons = document.getElementsByClassName("btn");
for (btn of buttons) {
    btn.addEventListener("click", (event) => {
  
    if(event.target.classList.contains('follow')){
      event.target.classList.remove('follow');
      event.target.innerHTML ="Following";
      event.target.style.backgroundColor = 'green' ;
    }else{
      event.target.classList.add('follow');
      event.target.style.backgroundColor = 'rgb(27,18,83)' ;
      event.target.innerHTML="Follow";
    }
    
  });
}
<button class="btn">1</button>
<button class="btn">2</button>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文