我尝试使用以下命令获取图像src的值:
<img onclick='swap(this); return false;' style='width:auto;max-width:65px;max-height:55px' src="/member/sthumb/$image_path_array[$i]">
所以当我点击它时,我得到这个值/member/sthumb/$image_path_array[$i]
这是我的javascript代码:
<script type="text/javascript">
function swap(image) {
...some code to cut the sthumb/ part from the string
document.getElementById("main").src = image.src;
}
</script>
在这个javascript代码中,我想删除此特定部分sthumb/
,以便image.src现在为:/member/$image_path_array[$i]
答案 0 :(得分:1)
function swap(image) {
//...some code to cut the sthumb/ part from the string
document.getElementById("main").src = image.src.replace('/sthumb', '');
}
答案 1 :(得分:0)
You can use simple replace method of String or it can be done using substring also
Solution1(Simple and Best):
function swap(image) {
document.getElementById("main").src = image.src.replace('/sthumb', '');
}
Solution2:
function swap(image) {
var str=image.src;
var StringToCut='/sthumb';
document.getElementById("main").src = str.substring(0,str.indexOf(StringToCut))+str.substring(str.indexOf(StringToCut)+StringToCut.length);
}