猿问

当 axios response.data 放在 dom 上时,所有点击事件都消失了

document.getElementById('listCategorie').addEventListener('click', (e) => {

    axios.get('http://localhost:3001/file/categorieList', {})

    .then((response) => {

    document.body.innerHTML = '';

    document.body.innerHTML = response.data

})

}) 

我知道如果我使用$(document).on(event,..,..)它效果很好,但不明白为什么。我根本不喜欢使用 jquery,只喜欢 vanilla js。


我想要的是,当 axios 响应放入 DOM 时,所有事件都会继续工作......但它们会消失。


呼啦一阵风
浏览 113回答 1
1回答

慕姐8265434

document.body.innerHTML = something || '';是更换你的整个身体。因此,在 head 标签内添加您的“js 脚本”<html>&nbsp; <head>&nbsp; &nbsp;<script>...</script> //like this&nbsp; </head><body>...</body></html>或者否则在特定元素中添加响应而不是正文document.getElementById('listCategorie').addEventListener('click', (e) => {&nbsp; &nbsp; axios.get('http://localhost:3001/file/categorieList', {})&nbsp; &nbsp; .then((response) => {&nbsp; &nbsp; document.getElementById('app').innerHTML = '';&nbsp; &nbsp; document.getElementById('app').innerHTML = response.data})})
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答