如何使用jQuery获取图像名称? [英] How to get an image name using jQuery?

查看:100
本文介绍了如何使用jQuery获取图像名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说:

<img src='static/images/banner/blue.jpg' />

使用jQuery,我如何获取blue数据?

Using jQuery, how could I get the blue data ?

如果使用$('img').attr('src'),则可以获取整个URI.在这种情况下,删除扩展名和所有路径的最佳方法是什么?

If I use $('img').attr('src'), I can get the whole URI. In that case, what is the best way to remove the extension and all the path ?

推荐答案

这里有一些问题-本地文件可能在路径名中使用其他斜杠('\'),并且某些文件名可以定义哈希或搜索尾,或者没有扩展名.

There are a couple gotcha here- local files may use the other slash ('\') in the pathname, and some filenames can have hash or search tails defined, or not have an extension.

String.prototype.filename=function(extension){
    var s= this.replace(/\\/g, '/');
    s= s.substring(s.lastIndexOf('/')+ 1);
    return extension? s.replace(/[?#].+$/, ''): s.split('.')[0];
}

console.log($('img').attr('src').filename());

这篇关于如何使用jQuery获取图像名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆