将axios response.data放到dom上时,所有单击事件都消失了

问题描述 投票:0回答:1
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,只有香草js。我希望将axios响应放入dom中后,所有事件都会继续进行,但它们会消失。

javascript axios
1个回答
0
投票

document.body.innerHTML = something || '';

正在替换您的整个身体。因此,请在head标签内添加“ js脚本”

<html>
  <head>
   <script>...</script> //like this
  </head>
<body>...</body>
</html>

否则将响应添加到特定元素而不是正文中

document.getElementById('listCategorie').addEventListener('click', (e) => {
    axios.get('http://localhost:3001/file/categorieList', {})
    .then((response) => {
    document.getElementById('app').innerHTML = '';
    document.getElementById('app').innerHTML = response.data
})
})
© www.soinside.com 2019 - 2024. All rights reserved.