【问题标题】:Why are my properties losing definition? (JS)为什么我的属性失去定义? (JS)
【发布时间】:2011-09-30 17:49:48
【问题描述】:

我正在创建一个拖放重页并为每个可拖动元素提供defaultTopdefaultLeft 属性,以便我可以轻松地将其重置为原始位置。直到突然,reset 函数将它们的值报告为未定义,这很好且不起眼。因为当它坏掉时我没有在处理这些部件(他们已经工作了一段时间)我不确定我哪里出错了。感谢您的帮助。

创建元素的函数是:

function makeTool(id, imgURL, defaultL, defaultT,trayname){
var a=document.createElement('img');
a.setAttribute('src', imgURL);
a.setAttribute('id',id);
a.setAttribute('class','drag');
a.intray=true;
a.tray=trayname;
a.rotation=0;
a.style.zIndex=document.getElementById(trayname).style.zIndex+1;
document.getElementById(trayname).appendChild(a);
a.defaultTop=ExtractNumber(document.getElementById(trayname).style.top)+defaultT;
a.defaultLeft=ExtractNumber(document.getElementById(trayname).style.left)+defaultL;
a.style.top=a.defaultTop+'px';
a.style.left =a.defaultLeft+'px';
a.addEventListener('touchstart', function(e) { return self.touchStart(e) }, false);
a.addEventListener('gesturestart', function(e) { return self.gestureStart(e) }, false);
a.addEventListener('touchmove', function(e) { return self.touchMove(e) }, false);
a.addEventListener('touchend', function(e) { return self.touchEnd(e) }, false);
tools.push(a);
return a;
}

而reset函数是:

function reposition(t){
    console.log(t+' top: '+document.getElementById(t).defaultTop);
    document.getElementById(t).style.left=document.getElementById(t).defaultLeft+'px';
    document.getElementById(t).style.top=document.getElementById(t).defaultTop+'px';
}

这是我得到undefined日志的地方

【问题讨论】:

  • 只是出于兴趣:你为什么要从头开始写这么多?为什么不使用像 jQuery 这样的库来为您完成所有繁重的工作?
  • 主要是因为我边走边学,而且我有一段时间没有使用 jQuery,所以它并没有像其他方式那样节省时间
  • 您是否尝试过console.log(a.defaultTop) a.defaultTop 的定义?是否从一开始就分配了undefined
  • 在您的 makeTool 函数日志中设置 a.defaultTop 值。那里定义正确吗?
  • @Anton: 是也不是...我创建了 n 个工具,然后我遍历了工具数组,记录了这些值,并且它们设置得很好。但是我在使用 document.getelementbyid 时没有做同样的事情。我现在在用手机,所以不能立即检查,但我会的。真正困扰我的是它曾经可以工作(现在仍然可以在 iPad webapp 中使用,但不能在移动 safari 中使用)

标签: javascript attributes properties


【解决方案1】:

我不确定 thar 是否有帮助,但我遇到了类似的问题。

我正在编写一个对象来根据我的 innerWidth 计算我的 DIV 的宽度:

function ScreenGrid() {
'use strict';

this.ColumnIDs = new Array();
this.Widths = new Array();
this.Dynamic = true;
this.Set = adjust;

var ColumnIDsPersist;
var WidthsPersist;

function adjust() {

    //stuff

    for (var i = 0; i < ColumnIDsPersist.length; i++) {

调整功能是神奇发生的地方。为了确保 DIV 始终适合屏幕,我使用了以下代码:

if (window.addEventListener) {
            // Good browsers.
            window.addEventListener('resize', react, false);
        }
        else if (w.attachEvent) {
            // Legacy IE support.
            window.attachEvent('onresize', react);
        }
        else {
            // Old-school fallback.
            window.onresize = react;
        }

这是反应函数:

    // Slight delay.
function react() {

    // Clear the timer as window resize fires,
    // so that it only calls adapt() when the
    // user has finished resizing the window.
    clearTimeout(timer);

    // Start the timer countdown.
    timer = setTimeout(adjust, 16);
    // -----------------------^^
    // Note: 15.6 milliseconds is lowest "safe"
    // duration for setTimeout and setInterval.
    //
    // http://www.nczonline.net/blog/2011/12/14/timer-resolution-in-browsers
}

问题是每次 REACT 触发时,o 在 this.ColumnIDs 中都未定义。经过大量调查后,我注意到当我从 SET() 触发 ADJUST 时,对象 THIS 是我的 SCREENGRID,但是当我从 REACT 触发时它变成了窗口,所以我在 this.ColumnIDs 中没有定义。

我用这段代码解决了:

function ScreenGrid() {
'use strict';

this.ColumnIDs = new Array();
this.Widths = new Array();
this.Dynamic = true;
this.Set = adjust;

var ColumnIDsPersist;
var WidthsPersist;

function adjust() {

    // This clearTimeout is for IE.
    // Really it belongs in react(),
    // but doesn't do any harm here.
    clearTimeout(timer);

    // Parse browser width.
    var _width = window.innerWidth || window.document.documentElement.clientWidth || window.document.body.clientWidth || 0;
    var _heigth = window.innerHeight || window.document.documentElement.clientHeight || window.document.body.clientHeight || 0;
    var _usedWidth = 0;
    var _fluidColumn = null;


    //Ao chamar a função adjust novamente no evento on resize, o objeto THIS deixa de ser a função e passa a ser a janela,
    //Essas vareáveis garantem a persistencia dos dados a serem utilizados.
    ColumnIDsPersist = (typeof ColumnIDsPersist == 'undefined') ? this.ColumnIDs : ColumnIDsPersist;
    WidthsPersist = (typeof WidthsPersist == 'undefined') ? this.Widths : WidthsPersist;

    for (var i = 0; i < ColumnIDsPersist.length; i++) {

好了,就是这样,希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 2013-02-26
    • 2013-05-21
    • 1970-01-01
    • 2021-06-30
    相关资源
    最近更新 更多