我有以下行:如何在JavaScript中的URL路径中呈现变量?
var myCustomVariable = '3434';
urlpath = '/people/myCustomVariable/folders/byid/'
我想渲染myCustomVariable在urlpath价值,但作为新的JS我无法想出解决办法。我试着做了以下但没有工作:
"+myCustomVariable+"!"
我在做什么错?
我有以下行:如何在JavaScript中的URL路径中呈现变量?
var myCustomVariable = '3434';
urlpath = '/people/myCustomVariable/folders/byid/'
我想渲染myCustomVariable在urlpath价值,但作为新的JS我无法想出解决办法。我试着做了以下但没有工作:
"+myCustomVariable+"!"
我在做什么错?
您使用+
经营者:“字符串连接”
var myCustomVariable = '3434';
urlpath = '/people/' + myCustomVariable + '/folders/byid/'
这就是所谓的“串联”或(因为我们正在处理字符串处理)
您的“我尝试了以下操作......”使用双引号和!
。我不确定!
来自哪里,但在JavaScript中,如果您打开带有单引号的字符串,则必须以单引号结尾;如果用双引号打开它,则必须用双引号结尾。
我只是将这个人发送给关于连接的文档 – 2014-08-28 21:09:57
@meanIOstack:一个例子经常比世界上所有的教程都值得。所以我用一个例子*和*来搜索。 :-) – 2014-08-28 21:11:28
只是做字符串的连接是这样的:
var myCustomVariable = '3434';
urlpath = '/people/' + myCustomVariable + '/folders/byid/'
当你这样做:
"+myCustomVariable+"
这代表着一个字符串,而不是你的变量。您的变量是
myCustomVariable
没有"
和身边这
看到这个:
var myCustomVariable = '3434';
//This
urlpath = '/people/' + myCustomVariable + '/folders/byid/'
//Same than
urlpath = '/people/' + '3434' + '/folders/byid/'
//Same than
urlpath = '/people/3434/folders/byid/'
但
var myCustomVariable = '3434';
//This
urlpath = '/people/' + '+myCustomVariable+' + '/folders/byid/'
//Same than
urlpath = '/people/+myCustomVariable+/folders/byid/'
这样你的意思是'urlpath = '/人/' + myCustomVariable ='/ folders/byid /'' – Dalorzo 2014-08-28 21:08:19
相关文档:https://developer.mozilla.org/en-US/docs/Web/J avaScript/Guide/Expressions_and_Operators#String_operators – 2014-08-28 21:11:34