說我有一個字符串數組。我想用一個固定的(公共)字符串來預先給每個字符串加上一個字符串。 我知道在Javascript中,字符串可以像strM = str1 + str2 + str3 + ... + strN
或strM = concat(str1, str2, str3, ..., strN)
連接起來。考慮這段代碼。如何預先使用'this'字符串
var defImgDirPath = 'res/img/';
$([
'home-icon-dark.png',
'home-icon-light.png'
]).each(function() {
/*
* Prepend each string with defImgDirPath
*/
});
現在,我不能做this = defImgDirPath + this;
(我是愚蠢到試圖)
而且,我想return (defImgDirPath + this);
但將不能工作。
我在想像this.prependString(defImgDirPath);
這樣的函數,但是有這樣一個函數嗎?如果不是,我該如何寫一個?
注意:我知道這可以通過for
循環很容易和簡單地完成,但有什麼好玩的呢? :)
我可能是模糊與你在問什麼,但你什麼時候需要使用它的路徑?你是否試圖在img標籤中使用它?如果路徑前綴是常量,您是否可以在需要使用它時連接? – Bmo 2013-03-28 10:46:53
您必須將參數傳遞給'.each' http://jsfiddle.net/hj2XJ/ – sofl 2013-03-28 10:47:54
使用jQuery並不總是有意義的...當您可以使用常規js時,您應該。 – 2013-03-28 10:52:07