
if (this.is == true)
alert('a')
if (this == document.getElementById('testdiv'))
alert("s")
}
你自己都写了this == document.getElementById('testdiv')
你在init方法里面绑定了onmousedown 方法,也没有做出改变,那么this肯定是指向this.myObj
this.is其实就是this.myObj.is,那肯定是undefined
所以如果你的is如果不是类外面需要用的话,可以直接封装起来
如下
function move2() {
var is = false//不用this.is,直接var is
this.myObj
//如果外部需要用到is,直接给move添加一个方法this.getIS = function(){return is}就好了
this.getIS = function(){return is}
this.startMove = function(evt) {
if (is == true)
alert('a')
if (this == document.getElementById('testdiv'))
alert("s")
}
this.init = function(obj) {
this.myObj = typeof obj == "string" ? document.getElementById(obj) : obj
is = true
this.myObj.onmousedown = this.startMove// 鼠标按下事件。
}
}
var mo = new move2()
mo.init("testdiv")
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)