当事件监听器直接添加到 HTML 文件中时如何删除它

发布于 2025-01-10 21:40:11 字数 1571 浏览 0 评论 0原文

有一个按钮,我要使用innerHTML添加到页面中,该按钮上有一个事件侦听器onclick="addWatchlist()",在下面,我将从按钮中删除该事件通过另一个事件侦听器,但我无法使用 removeEventListener("click",addWatchlist) 来完成此操作。 我已阅读:

  1. 为什么removeEventListener不起作用?
  2. Javascript removeEventListener 不工作
    它们是关于删除使用 javascript addEventListener 添加的事件。但这里我们将事件直接添加到 HTML 中。
<button class="watch-list-btn" onclick="addWatchlist()">Watchlist </button>
watchlist.addEventListener("click",function(){
    conatinerDown.innerHTML =""
    conatinerDown.innerHTML +=movieWatchlistArray.map(each=> {       
        return each.outerHTML
    }).join("")
// loop through  movie list and changes text and removes event listener.
    for(let i=0 ;i<moviePageNumber;i++ ){
        document.querySelector(`.movie-${i} .watch-list-btn`).textContent = "remove"
        document.querySelector(`.movie-${i} .watch-list-btn`).removeEventListener("click",addWatchlist)
    }
    
})

// adds movie to start of an array called  movieWatchlistArray
function addWatchlist(){
    
    movieWatchlistArray.unshift(document.querySelector(`.movie-${moviePageNumber-1}`))
    console.log(movieWatchlistArray)
    
}

有没有办法删除该事件?无需将 HTML 更改为以下内容:

<button class="watch-list-btn">Watchlist </button>

there is a button that I am going to add to the page with innerHTML the button has an eventlistener on it onclick="addWatchlist()" and in bellow, I am going to remove the event from the button by another event listener but I can't do it with removeEventListener("click",addWatchlist).
I have readen:

  1. why doesn't removeEventListener work?
  2. Javascript removeEventListener not working
    they are about removing events that are added with javascript addEventListener.but here we add the event directly into HTML.
<button class="watch-list-btn" onclick="addWatchlist()">Watchlist </button>
watchlist.addEventListener("click",function(){
    conatinerDown.innerHTML =""
    conatinerDown.innerHTML +=movieWatchlistArray.map(each=> {       
        return each.outerHTML
    }).join("")
// loop through  movie list and changes text and removes event listener.
    for(let i=0 ;i<moviePageNumber;i++ ){
        document.querySelector(`.movie-${i} .watch-list-btn`).textContent = "remove"
        document.querySelector(`.movie-${i} .watch-list-btn`).removeEventListener("click",addWatchlist)
    }
    
})

// adds movie to start of an array called  movieWatchlistArray
function addWatchlist(){
    
    movieWatchlistArray.unshift(document.querySelector(`.movie-${moviePageNumber-1}`))
    console.log(movieWatchlistArray)
    
}

is there a way to remove the event? without changing the HTML to something like :

<button class="watch-list-btn">Watchlist </button>

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

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

发布评论

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

评论(2

等待圉鍢 2025-01-17 21:40:11

您可以获取该元素并将 onclick 属性设置为 null。

function addWatchlist() {
  console.log('addWatchlist');
}

function removeListener() {
  const button = document.getElementsByClassName('watch-list-btn')[0];
  button.setAttribute('onclick', null);
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/marx/4.0.0/marx.min.css" rel="stylesheet"/>
<button class="watch-list-btn" onclick="addWatchlist()">Watchlist</button>
<button onclick="removeListener()">Remove Listener</button>

You can get the element and set the onclick attribute to null.

function addWatchlist() {
  console.log('addWatchlist');
}

function removeListener() {
  const button = document.getElementsByClassName('watch-list-btn')[0];
  button.setAttribute('onclick', null);
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/marx/4.0.0/marx.min.css" rel="stylesheet"/>
<button class="watch-list-btn" onclick="addWatchlist()">Watchlist</button>
<button onclick="removeListener()">Remove Listener</button>

三岁铭 2025-01-17 21:40:11
<button class="watch-list-btn" id="watch-list-btn">Watchlist </button>
<button class="watch-list-btn" id="remove-event">Watchlist </button>

现在你可以编写 javascript

let btn = document.getElementById('watch-list-btn');

function clickHandler(){
    console.log('Clicked')
}
btn.addEventListener('click', clickHandler);

// Remove event on button click
let remove = document.getElementById('remove-event');
remove.addEventListener('click', function(){
    btn.removeEventListener('click', clickHandler);
});

这段代码将适合你。

<button class="watch-list-btn" id="watch-list-btn">Watchlist </button>
<button class="watch-list-btn" id="remove-event">Watchlist </button>

now you can write the javascript

let btn = document.getElementById('watch-list-btn');

function clickHandler(){
    console.log('Clicked')
}
btn.addEventListener('click', clickHandler);

// Remove event on button click
let remove = document.getElementById('remove-event');
remove.addEventListener('click', function(){
    btn.removeEventListener('click', clickHandler);
});

This code will work for you.

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