jQuery/Javascript赋值左侧无效
jQuery/Javascript Invalid left-hand side in assignment
我使用的是这个相对简单的代码:
var height = help ? 'minus' : 'plus';
var prop = $('#properties');
if(height == 'minus'){
prop.height(prop.height() -= 206);
} else {
prop.height(prop.height() += 206);
}
它在做加法/减法的两行都失败了!有什么想法吗?
-=
运算符等于operand = operand - value
,在您的情况下看起来像
prop.height() = prop.height() - 206;
这显然会失败。您只需要使用减号运算符即可完成该任务。
prop.height(prop.height() - 206);
将执行此操作。
您不能-=方法。
要么你需要prop.height(prop.height() - 206);
或者先收集值,然后-=就像。。。
var h = prop.height();
h -= 206
prop.height( h);
prop.height() -= 206
尝试分配给返回值,这不是一个不可能的变量;与(prop.height() = prop.height() - 206
)相同
相反,你可以;prop.height(prop.height() - 206);
或(prop.height(prop.height() + (height === 'minus' ? -206 : 206));
)
var height = help ? 'minus' : 'plus';
var prop = $('#properties');
var propHeight = prop.height();
if(height === 'minus'){
prop.height(propHeight - 206);
} else {
prop.height(propHeight + 206);
}
您已经得到了答案,但我想提一下为什么要使用if/else来进行加法或减法:
// subtract height if help is true, otherwise add height
var heightmodifier = help ? -1 : 1;
var prop = $('#properties');
var propHeight = prop.height();
prop.height(propHeight + (206 * heightmodifier));
相关文章:
- 未捕获的ReferenceError:赋值中的左侧无效
- 变量赋值无效
- Javascript/Html 未捕获的引用错误:赋值中的左侧无效;未捕获的引用错误:未定义计算
- 样式属性赋值上的“赋值中的左侧无效”
- 左侧赋值无效(railo.transformer.bytecode.op.OpString)
- 在javascript中构建HTML表,得到“引用错误:左侧无效赋值”
- 更改 CSS“z-index”属性:“赋值中的左侧无效”
- Javascript 错误:左侧赋值无效
- 在引用错误中将字符串放在一起:左侧的赋值无效
- 未捕获的引用错误:赋值(匿名函数)中的左侧无效
- jQuery/Javascript赋值左侧无效
- 为innerHTML赋值无效
- 带有三进制if的左侧赋值无效
- 在我必须做的一件简单的事情中,赋值中的左侧无效
- "赋值中的左侧无效“;当接收到来自Facebook API的响应时
- 我得到了“;赋值中的左侧无效“;在我的代码(JS)中
- 变量名称-赋值中左侧无效
- 未捕获的ReferenceError:赋值javascript中的左侧无效
- 如何处理'无效赋值left hand '错误:firefox或chrome
- Javascript ReferenceError:左边无效赋值