几天前我写了一个简单的东西,如果无法加载所需的图像,则回退到占位符图像。它允许用户使用 2 个占位符来定义图像大小:
这就是我写的东西:
;(window.jQuery || window.Zepto).fn.fallback = function (url) {
return this.one('error', function () {
this.src = (url|| 'http://lorempixel.com/$w/$h')
.replace('$w', this.width).replace('$h', this.height);
});
};
我现在问我,是否可以.replace('$w', this.width).replace('$h', this.height);用更短但等于regex将所有$*(美元+第一个字符)替换为任何对象的赋值?
像这样的东西:
'$f is not equal to $b'.replace(/magicregex/, {
foo: 'foo',
bar: 'bar'
});
这样我们就可以使用所有properties来自image-object, 例如image.width, image.src, image.width...