我正在制作一个待办事项列表,当我将鼠标悬停在对应的李上时,我有一个显示(它增加不透明度)删除按钮的功能。
todosUl.addEventListener("mouseover", function(event) {
var elementHovered = event.target;
var deleteButton = document.getElementById("deleteButton"+elementHovered.id)
deleteButton.style.opacity=1;
});
todosUl.addEventListener("mouseout", function(event) {
var elementHovered = event.target;
var deleteButton = document.getElementById("deleteButton"+elementHovered.id)
deleteButton.style.opacity = 0;
});
问题是,每当我接近按钮区域时,按钮就会再次消失。发生这种情况时,控制台会显示:Cannot read property 'style' of null
这就是我创建 Li 的方式:
todoLi.appendChild(view.createDeleteButton(position));
createDeleteButton: function (liId) {
var deleteButton = document.createElement('button');
deleteButton.textContent = 'X';
deleteButton.className = 'deleteButton';
deleteButton.id = 'deleteButton'+liId;
return deleteButton;