说我有一个字符串数组。我想用一个固定的(公共)字符串来预先给每个字符串加上一个字符串。 我知道在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