如何在JavaScript中的字符串中放置变量?如何将变量放入使用javascript的字符串中
例子:
$('#contactForm').animate({"marginLeft": "+=680px"}, "slow");
我想在一个变量而不是680px
使用,是有办法做到这一点?
如何在JavaScript中的字符串中放置变量?如何将变量放入使用javascript的字符串中
例子:
$('#contactForm').animate({"marginLeft": "+=680px"}, "slow");
我想在一个变量而不是680px
使用,是有办法做到这一点?
与大多数没有信号的语言一样,您不能执行插值,您必须连接多个字符串。
"foo" + "bar" + "baz"
"foo" + string_var + "baz"
"+=" + string_var + "px"
可以使用+
运算符来连接你的价值在一个变量的"+="
字符串:
$('#contactForm').animate({"marginLeft": "+=" + size}, "slow");
var theWidth = "680px"
$('#contactForm').animate({marginLeft: "+=" + theWidth}, "slow");
var size = "680px";
$('#contactForm').animate({"marginLeft": "+="+size}, "slow");