简体   繁体   English

为什么在javascript中需要变量之间的+?

[英]Why do you need the + between variables in javascript?

Why does this line work 为什么这条线有效

$('#body-image').css("background-image", 'url('+ backgroundimage +')');

but not this one 但不是这个

$('#body-image').css("background-image", 'url('backgroundimage')');

or this one 或者这个

$('#body-image').css("background-image", 'url(backgroundimage)');

backgroundimage is a JavaScript variable. backgroundimage是一个JavaScript变量。 The concatenation operator in JavaScript is +, so to put a string together with a variable, you do 'some string ' + someVariable . JavaScript中的连接运算符是+,因此要将字符串与变量放在一起,您可以执行'some string ' + someVariable Without the +'s, JavaScript wouldn't know what to do with your variable (and in your third example, wouldn't even know that it was a variable). 没有+,JavaScript就不知道如何处理你的变量(在你的第三个例子中,甚至不知道它一个变量)。

You need to concat the string with the variable backgroundimage . 您需要使用变量backgroundimage字符串。 So you use "+" for this. 所以你用“+”来做这件事。

That's why this doesn't work. 这就是为什么这不起作用。

$('#body-image').css("background-image", 'url('backgroundimage')');

And the secont doesn't work because there is no image called 'backgroundimage'. 而secont不起作用,因为没有名为'backgroundimage'的图像。

$('#body-image').css("background-image", 'url(backgroundimage)');

Because you are building a string. 因为你正在构建一个字符串。 You are missing the line where backgroundimage gets a value: 您缺少backgroundimage获取值的行:

 var backgroundimage = "someimage.gif";
 $('#body-image').css("background-image", 'url('+ backgroundimage +')');  

becomes: 变为:

 $('#body-image').css("background-image", 'url(someimage.gif)');  

it's concatenating the string. 它连接字符串。 let's say backgroundimage is 'foo.jpg, then 让我们说backgroundimage是'foo.jpg,那么

'url('+backgroundimage+')'  =  'url(foo.jpg)'

In JavaScript, a string literal (ie, "I am a string") is actually treated like a String object (though, strictly speaking, it isn't - see the MDC documentation - but we can ignore the difference at this level). 在JavaScript中,字符串文字(即“我是一个字符串”)实际上被视为一个String对象(严格来说,它不是 - 请参阅MDC文档 - 但我们可以忽略此级别的差异)。 The following two lines are equivalent: 以下两行是等效的:

var letters = "ABC", numbers = "123";
var letters = new String("ABC"), numbers = new String("123");

Strings are concatenated using either the + operator or the String.concat method, either of which join 2 or more strings in a left-to-right order and return the result. 使用+运算符或String.concat方法连接字符串,其中任何一个以从左到右的顺序连接2个或更多字符串并返回结果。 So in order to get "ABC123", we can do any of the following: 因此,为了获得“ABC123”,我们可以执行以下任何操作:

"ABC" + "123"
"ABC" + numbers
letters + "123"
letters + numbers
"ABC".concat("123")
"ABC".concat(numbers)
letters.concat("123")
letters.concat(numbers)

but not: 但不是:

letters"123"
"ABC"numbers
lettersnumbers
"lettersnumbers"

which are all, effectively, the same thing that you were trying to do in your examples. 这些都与您在示例中尝试的内容完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM