简体   繁体   English

Javascript什么是“'+ ​​aStringVarible +'”(双引号加单引号)呢?

[英]Javascript What does “' + aStringVarible + '” (double quote plus single quote) do?

I saw this line of code from an online course: 我从在线课程中看到了这行代码:

photoHtml += '<a href="' + photo.link + '">';

Here, "photo" is an object, and photo.link is actually a url. 这里,“照片”是一个对象,而photo.link实际上是一个网址。

But it is my first time seeing people use "' together (I am new to javascript), so I am not sure what this does. My guessing is that it is a way to add quotations to a variable? Because if we directly put quotes around the variable photo.link, then it would not work as a variable, eg "photo.link"? 但这是我第一次看到人们一起使用''(我是javascript的新手),所以我不确定这是做什么的。我的猜测是,它是一种向变量添加引用的方法吗?因为如果我们直接输出引号围绕变量photo.link,然后它不会作为变量工作,例如“photo.link”?

In python, I would use '"' + photo.link + '"' to deal with the case. 在python中,我会使用'“'+ photo.link +'”来处理这个案例。 So does "' + photo.link + '" do the same job here in javascript? 那么“'+ photo.link +'”在javascript中做同样的工作吗?

Thank you in advance! 先感谢您! I know it is really amateur, but I tried to search on google, and did not find desired answers. 我知道它真的很业余,但我试图在谷歌搜索,并没有找到理想的答案。

You can define string literals either like this 'a string' or "a string" . 您可以将字符串文字定义为'a string'"a string" Both allow the other kind of character to be included without an escape character: '"hello" world' is a string containing "hello" world . 两者都允许包含其他类型的字符而没有转义字符: '"hello" world'是一个包含"hello" world的字符串。

The valid HTML produced by this will be something like 由此产生的有效HTML将是类似的

<a href="http://something.com/">

Notice that the quotes " delimit the link url . 请注意,引号"分隔链接URL

Now, in JavaScript, regardless of the content of a string, the string in photoHTML is delimited by the quotes ' . 现在,在JavaScript中,无论字符串的内容如何, photoHTML的字符串都用引号'分隔。 Thus, if you want your url to be a variable, you must somehow place it such that the produced html looks like this: 因此,如果你想让你的url成为一个变量,你必须以某种方式放置它,使得生成的html看起来像这样:

<a href="photo.link">

Where you want to replace the text photo.link with your actual link. 您想要将文本photo.link 替换为您的实际链接。 This is achieved by: 这通过以下方式实现:

photoHtml += '<a href="' + photo.link + '">';

where the " quotes delimit the url in the html and the ' quotes delimit the string in the JavaScript . 其中"引号分隔html中的url和'引号分隔JavaScript中的字符串。

The " has nothing to do with it the concatenation. The " is for the attribute that is being rendered. "与连接无关。 "是针对正在呈现的属性。

<a href="thelink">
        ^       ^

The value of the href attribute has to be enclosed by quotation marks. href属性的value必须用引号括起来。 It doesn't matter if it's a ' single or a " double quotation mark. 它是'单引号"还是"双引号"并不重要。

In your example the ' marks beginning and end of the javascript expression (string concatenation) while the " refers to the html attribute href and is part of the string itself. 在您的示例中, '标记javascript表达式的开头和结尾(字符串连接),而"引用html属性href并且是字符串本身的一部分。

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

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