我不知道我哪里做错了。 我有两个听众,一个连接到祖先容器,第二个是它里面的按钮。 我的目标是一个带有 .closest 方法的按钮,它也会触发子级和祖先级。 我试着把
event.stopPropagation
给儿童听众,但它不起作用。
containerWorkouts.addEventListener('click', this._moveToPopup.bind(this));
containerWorkouts.addEventListener('click', this._removeWorkout.bind(this));
_moveToPopup(e) {
console.log('move');
const workoutEl = e.target.closest('.workout');
if (!workoutEl) return;
const workout = this.#workouts.find(
work => work.id === workoutEl.dataset.id
);
this.#map.setView(workout.coords, this.#mapZoomLevel, {
animate: true,
pan: { duration: 1 },
});
}
_removeWorkout(e) {
const deleteBtn = e.target.innerText;
if (deleteBtn === 'Delete') {
e.stopPropagation();
const curWorkout = e.target.closest('.workout');
const currentID = curWorkout.getAttribute('data-id');
const indexWorkout = this.#workouts.findIndex(
wor => wor.id === currentID
);
console.log(indexWorkout);
this.#workouts.splice(indexWorkout, 1);
console.log(this.#workouts);
curWorkout.remove();
}
}