开发者

Javascript array error

I am trying to make one array equal another array like so: elementStyle[property] = styles[property];

But i keep getting a "invalid argument" error.

Any Ideas????

Addtional:

The code is on: http://prototypejs.org/assets/2009/8/31/prototype.js. The area that i'm interested in is:

setStyle: function(element, styles) {
  element = $(element);
  var elementStyle = element.style, match;
  if (Object.isString(styles)) {
    element.style.cssText += ';' + styles;
    return styles.include('opacity') ?
      element.setOpacity(styles.match(/opacity:\s*(\d?\.?\d*)/)[1]) : element;
  }
  for (var property in styles)
    if (property == 'opacity') element.setOpacity(styles[property]);
    el开发者_如何学Gose
      elementStyle[(property == 'float' || property == 'cssFloat') ?
        (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') :
          property] = styles[property];

  return element;
},

This code is fine in Firefox and Chrome, but not in IE. I get an invalid argument error.


Correct me if I'm wrong, but if you pass in an Object like an Array to styles, the for(var property in styles) will return functions and the conditions do not check for that.

You'll want to put in a check after your for:

for (var property in styles)
   if (typeof property == "string") // should filter out functions/objects/etc.

I'm not sure if this is what the cause of your error is though. I'm not sure what happens if you try assigning a function to the element.style.


el.style property is an object, not an Array... by the way in webkit it's not enumerable... If you try to iterate over style propertyes try it with a fixed list.

props = ('backgroundColor borderBottomColor borderBottomWidth borderLeftColor borderLeftWidth '+
        'borderRightColor borderRightWidth borderSpacing borderTopColor borderTopWidth bottom color fontSize '+
        'fontWeight height left letterSpacing lineHeight marginBottom marginLeft marginRight marginTop maxHeight '+
        'maxWidth minHeight minWidth opacity outlineColor outlineOffset outlineWidth paddingBottom paddingLeft '+
        'paddingRight paddingTop right textIndent top width wordSpacing zIndex').split(' ');
for (var i=0, len=props.length; i<len; i++) 
        element.style[props[i]] = styles[props[i]];

Another typical bug is that when you want to access border-bottom-width, you must replace /-([a-z])/g to uppercase letters...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜