当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,只喜欢使用香草js。

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

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

是替换你的整个身体。因此,在头标签中添加您的"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
})
})

最新更新