在addBlocker函数中,我从按钮中删除了onClick属性。 在removeBlocker函数中,我需要该属性以及在html文件中实现的处理程序函数。
(我尝试了另一种使用element.style.pointerEvents ='none'的方法) 但是那样,我无法从事件中获取clientX和clientY值。将pointEvents设置为none时,有什么方法可以获取clientX和clientY?
document.addEventListener('mouseover', function(e) {
if (e.target.type === 'button') {
newBody[i].removeAttribute('onClick');
}
newBody[i].addEventListener('click', e => {
e.preventDefault();
});
const { clientX, clientY } = e;
// newBody[i].style.pointerEvents = 'none';
// const elementMouseIsOver = document.elementFromPoint(clientX, clientY);
let elementMouseIsOver = document.querySelectorAll(':hover');
elementMouseIsOver = elementMouseIsOver[elementMouseIsOver.length - 1];
console.log({ clientX, clientY, elementMouseIsOver });
});
我希望在运行另一个函数时取回删除的onClick属性,或者在style.pointEvents为none时找到获取e.clientX和e.clientY的方法。
答案 0 :(得分:0)
尝试在mouseleave
上添加活动
document.addEventListener('mouseleave', function(e) {
if (e.target.type === 'button') {
newBody[i].setAttribute('onClick', functionName);
}
})
答案 1 :(得分:0)
如果以后需要恢复属性值,则必须在删除属性并将其存储在某处之前对其进行检索。
声明一个存储位置:
var buttonClick;
您要删除它的地方
buttonClick = newBody[i].getAttribute('onClick');
要还原的位置:
newBody[i].setAttribute('onClick', buttonClick);
您甚至可以通过使用 expando属性(基本上,您可以肯定不会与其他人冲突的名称)来将其存储在元素本身上:
newBody[i].__my_blocker_buttonClick = newBody[i].getAttribute('onClick');
然后将其还原到哪里:
newBody[i].setAttribute('onClick', newBody[i].__my_blocker__buttonClick);