我有以下行:如何在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