Javascript数组错误
我试图使一个数组与另一个数组相等,如下所示:Javascript数组错误,javascript,Javascript,我试图使一个数组与另一个数组相等,如下所示:elementStyle[property]=style[property] 但我不断地得到一个“无效参数”错误 有什么想法吗 附加: 代码位于:。我感兴趣的领域是: setStyle: function(element, styles) { element = $(element); var elementStyle = element.style, match; if (Object.isString(styles)) { el
elementStyle[property]=style[property]代码>
但我不断地得到一个“无效参数”错误
有什么想法吗
附加:
代码位于:。我感兴趣的领域是:
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]);
else
elementStyle[(property == 'float' || property == 'cssFloat') ?
(Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') :
property] = styles[property];
return element;
},
这段代码在Firefox和Chrome中没有问题,但在IE中没有问题。我得到一个无效的参数错误。el.style属性是一个对象,而不是数组。。。顺便说一下,在webkit中它不是可枚举的。。。
如果您尝试迭代样式属性Yes,请使用固定列表进行尝试
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]];
props=('backgroundColor borderboundboundcolor borderboundboundwidth borderLeftColor borderLeftWidth'+
“borderRightColor borderRightWidth borderSpacing borderTopColor borderTopWidth底色fontSize”+
'字体重量高度左侧字母间距线宽边距底部边距左侧边距右侧边距插入最大高度'+
'最大宽度最小高度最小宽度不透明度大纲颜色大纲新偏移大纲宽度填充底部填充左'+
“paddingRight paddingTop right text缩进顶部宽度字间距zIndex”).split(“”);
对于(var i=0,len=props.length;iel.style属性是一个对象,而不是数组…顺便说一句,在webkit中,它是不可枚举的。。。
如果您尝试迭代样式属性Yes,请使用固定列表进行尝试
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]];
props=('backgroundColor borderboundboundcolor borderboundboundwidth borderLeftColor borderLeftWidth'+
“borderRightColor borderRightWidth borderSpacing borderTopColor borderTopWidth底色fontSize”+
'字体重量高度左侧字母间距线宽边距底部边距左侧边距右侧边距插入最大高度'+
'最大宽度最小高度最小宽度不透明度大纲颜色大纲新偏移大纲宽度填充底部填充左'+
“paddingRight paddingTop right text缩进顶部宽度字间距zIndex”).split(“”);
for(var i=0,len=props.length;i如果我错了,请纠正我,但是如果您将类似数组的对象传递给styles,则for(styles中的var属性)将返回函数,并且条件不会对此进行检查
您需要在完成以下操作后进行检查:
for (var property in styles)
if (typeof property == "string") // should filter out functions/objects/etc.
我不确定这是否是导致错误的原因。我不确定如果尝试将函数分配给元素.style会发生什么情况。如果我错了,请更正我,但如果将数组之类的对象传递给样式,则for(样式中的var属性)将返回函数,并且条件不会对此进行检查
您需要在完成以下操作后进行检查:
for (var property in styles)
if (typeof property == "string") // should filter out functions/objects/etc.
但是,我不确定这是否是导致错误的原因。我不确定如果您尝试将函数分配给元素.style.您可以发布代码吗?有许多可能的选项,如果没有看到代码,您可能永远无法得到正确的答案。属性
从何而来?按照惯例,只要您在您的对象/数组中包含元素,其键/名称应为属性中的任何内容。我们需要查看更多代码,但我认为elementStyle或styles未定义或不存在。请显示一些实际代码。也许您的意思是说elementStyle['property']=styles['property']你能发布代码吗?有很多可能的选项,如果没有看到代码,你可能永远不会得到正确的答案。property
从何而来?按照惯例,只要你的对象/数组中的元素具有属性中任何内容的键/名称,它就应该工作。我们需要看到更多的代码,但我想elementStyle或styles未定义或不存在。请显示一些实际代码。可能您的意思是说elementStyle['property']=styles['property']