js事件问题

时间:2011-04-11 02:29:29

标签: javascript javascript-events

我想像任何一个想法一样成为财产检查员之王。这是我的代码(正在开发中)

var PropertiesWindow = function(divId, title) {
            this.propList = new Array();
            this.postfix = new Date().getTime();
            this.observedObject = null;

            this.containerDiv = divId;
            this.propIdSufix = 'prop_';
            title = title != undefined ? title : 'Properties';
            // create the header element
            var header = document.createElement('div');
            // set same style
            header.id = 'prop-header'+this.postfix;
            header.innerHTML = format('<span>{0}</span>', title);
            document.getElementById(divId).appendChild(header);
            this.domElement = document.getElementById(this.containerDiv);
            // set style
        };

        PropertiesWindow.prototype.propertyValueChanged = function(ev){
            if(ev!=null && this.observedObject!=null){              
                var propName = ev.target.id.split('_')[0];
                this.observedObject[propName] = ev.target.value;
            }
        };

        PropertiesWindow.prototype.attachProp = function(id, description,
                editor) {
            if (this.domElement != null) {
                var content = '<label style="margin-left:7px;margin-bottom:7px;float:left;">{0}</label><input id="{1}" type="text" style="border: solid 1px #aaaaaa;float:right;margin-right:7px;width: 70px;" />';
                content = format(content, description, id+'_'+this.postfix);
                var divElement = document.createElement('div');

                divElement.id = this.propIdSufix + id;
                divElement.style.clear = 'both';
                divElement.innerHTML = content;             
                this.propList.push(id);
                this.domElement.appendChild(divElement);
                document.getElementById(id+'_'+this.postfix).addEventListener('keyup', this.propertyValueChanged, false);
            }
        };

        PropertiesWindow.prototype.removeProp = function(id) {
            if (this.domElement != null) {
                var propId = this.propIdSufix + id;
                var innerElement = document.getElementById(propId);// this.findProp(propId);

                if (innerElement != null)
                    this.domElement.removeChild(innerElement);
            }
        };


        PropertiesWindow.prototype.subscribe = function(obj) {
            obj.setListener(this);
            this.observedObject = obj;
            for ( var propName in obj) {
                var propElement = document.getElementById(propName+'_'+this.postfix);
                if (typeof (obj[propName]) != "undefined"
                        && propElement != null) {
                    propElement.value=obj[propName];
                }
            }
        };

        PropertiesWindow.prototype.update = function(pname,val) {
                var propElement = document.getElementById(pname+'_'+this.postfix);
                if(propElement!=null)
                    propElement.value = val;
    };

我在方法attachProp上遇到问题我有这段代码document.getElementById(id+'_'+this.postfix).addEventListener('keyup', this.propertyValueChanged, false);

我在对象调用propertyValueChanged中定义了一个方法 但是,当它被触发时,我无法访问PropertiesWindow的属性。我明白我在其他情况下。有没有可以访问PropertiesWindow的属性??

外观:

if(ev!=null && this.observedObject!=null){              
                var propName = ev.target.id.split('_')[0];
                this.observedObject[propName] = ev.target.value;
            }

this.observedObject未定义

提前致谢。

1 个答案:

答案 0 :(得分:0)

var self = this;

document.getElementById(id+'_'+this.postfix).addEventListener('keyup', function(evt) {
    self.propertyValueChanged(evt);
}, false);