为了账号安全,请及时绑定邮箱和手机立即绑定

参数怎么填解除事件绑定需要函数名,但是如果给出函数名this又发生了变化 ?为什么

参数怎么填解除事件绑定需要函数名,但是如果给出函数名this又发生了变化 ?为什么

HUX布斯 2022-06-15 17:11:16
<!DOCTYPE><html><head><title></title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/><meta name="format-detection" content="telephone=no"/><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="white"><style type="text/css">* { padding:0; margin:0;}#div1 { width:100px; height:100px; background-color:Red; position:absolute;}</style><script language="javascript" type="text/javascript">function PhoneDrag(id) {var _this = this;this.oDiv = document.getElementById(id);this.disX = 0;this.disY = 0;this.oDiv.addEventListener("touchstart", function (ev) {_this.fnDown(ev);}, false);}PhoneDrag.prototype.fnDown = function (ev) {var _this = this;var oEvent = ev.touches[0];ev.preventDefault();this.disX = oEvent.clientX - this.oDiv.offsetLeft;this.disY = oEvent.clientY - this.oDiv.offsetTop;document.addEventListener("touchmove", function (ev) {_this.fnMove(ev);}, false);document.addEventListener("touchend", function (ev) {//这里是手指抬起的时候 如何删除touchmove事件document.removeEventListerner(document, "这里怎么填");}, false);}PhoneDrag.prototype.fnMove = function (ev) {var oEvent = ev.touches[0];var iX = oEvent.clientX - this.disX;var iY = oEvent.clientY - this.disY;this.oDiv.style.top = iY + "px";this.oDiv.style.left = iX + "px";}window.onload = function () {new PhoneDrag("div1");}</script></head><body><div id="div1"></div></body></html>这是移动端移动DIV的例子
查看完整描述

2 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞


// 因为你注册事件时的第二个参数函数是匿名函数调用已存函数 // 我不知道这样写是否有效 你试下document.removeEventListerner("touchmove"function (ev) {                _this.fnMove(ev);            }, false);

只有被addEventListener方法添加的事件才可以使用removeEventListener来注销.
删除事件时removeEventListener的三个参数必须要跟addEventListener的参数相同

语法:elem.removeEventListener(event_type,fun_name,bool);

event_type:事件类型.比如单击,或双击.或移动鼠标事件等.
fun_name:要被注销的事件名称,函数名.
bool:布尔值.true或false.true代表支持事件冒泡.false则不支持事件冒泡捕获



查看完整回答
反对 回复 2022-06-20
?
慕码人8056858

TA贡献1803条经验 获得超6个赞


function PhoneDrag(id) {    var _this = this;    this.oDiv = document.getElementById(id);    this.disX = 0;    this.disY = 0;    this.oDiv.addEventListener("touchstart"function (ev) {        _this.fnDown(ev);    }, false); }PhoneDrag.prototype.fnDown = function (ev) {    var _this = this;    var oEvent = ev.touches[0];    ev.preventDefault();    this.disX = oEvent.clientX - this.oDiv.offsetLeft;    this.disY = oEvent.clientY - this.oDiv.offsetTop;    // 解除事件绑定的时候需要用到    var touchmoveHandle = function (ev) {        _this.fnMove(ev);    };    document.addEventListener("touchmove", touchmoveHandle, false);    document.addEventListener("touchend"function (ev) {        //这里是手指抬起的时候 如何删除touchmove事件        document.removeEventListerner('touchmove', touchmoveHandle, false);    }, false); }PhoneDrag.prototype.fnMove = function (ev) {    var oEvent = ev.touches[0];    var iX = oEvent.clientX - this.disX;    var iY = oEvent.clientY - this.disY;    this.oDiv.style.top = iY + "px";    this.oDiv.style.left = iX + "px";} window.onload = function () {    new PhoneDrag("div1");}




查看完整回答
反对 回复 2022-06-20
  • 2 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信