javascript onclick获取没有路径的图像名称 [英] javascript onclick get image name without path
本文介绍了javascript onclick获取没有路径的图像名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从没有路径的SRC属性复制图像的名称(名称+扩展名)..
HTML:
I need to copy the name of the image (name + extension) from SRC attribute without path.. HTML:
<input type="text" id="result" /><br /><br />
<img src="../some_folder/some_folder/photo_name.jpg" onclick="getName()" id="img1" />
JS:
function getName() {
document.getElementById("result").value = document.getElementById("img1").src;
}
此代码克隆图像的完整路径..路径不是静态的,所以我不能只是削减SRC的其余部分..
提前谢谢
This code clones full path of the image.. Path is not static, so I can not just cut rest of "SRC".. Thanks in advance
推荐答案
您应该尝试以下代码:
You should try this code:
var filename = fullPath.replace(/^.*[\\\/]/, '');
你的JS函数是:
function getName() {
var fullPath = document.getElementById("img1").src;
var filename = fullPath.replace(/^.*[\\\/]/, '');
// or, try this,
// var filename = fullPath.split("/").pop();
document.getElementById("result").value = filename;
}
这篇关于javascript onclick获取没有路径的图像名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文