jquery中获取图片的相对路径的方法。现为绝对路径img.src, // http://james.padolsey.com/javascript/parsing-urls-with-the-dom/// This function creates a new anchor element and uses location// properties (inherent) to get the desired URL data. Some String// operations are used (to normalize results across browsers). function parseURL(url) {
var a =
document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function(){
var ret = {},
seg = a.search.replace(/^\?/,'').split('&'),
len = seg.length, i = 0, s;
for (;i
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
hash: a.hash.replace('#',''),
path: a.pathname.replace(/^([^\/])/,'/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
segments: a.pathname.replace(/^\//,'').split('/')
};}console.log( parseURL('//sfault-avatar.b0.upaiyun.com/337/185/3371853797-551119fea53e1_big64') );
这篇博客介绍了在jQuery中如何获取图片的相对路径,当前方法返回的是绝对路径。通过创建一个新锚点元素并利用其location属性来解析URL。文章提供了详细的函数示例,用于将URL转换为可操作的对象,包括协议、主机名、端口、查询参数等。此外,还展示了如何从URL中提取文件名和路径信息。
181

被折叠的 条评论
为什么被折叠?



