<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script type="text/javascript">
function m()
{
var d = document.getElementById('img1');
alert(d.src);//结果输出了网址加index.jpg
//请问:如何能只输出字符串:index.jpg
}
</script>
<body>
<img id="img1" src="index.jpg" onclick="m()" />
</body>
</html>

解决方案 »

  1.   


    alert(d.getAttribute('src'));
      

  2.   

    这样试试:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head>
    <script type="text/javascript">
    function m() {
    var d = document.getElementById('img1');
    alert(d.getAttribute("src"));//结果输出了网址加index.jpg
    //请问:如何能只输出字符串:index.jpg
    }
    </script>
    <body>
    <img id="img1" src="index.jpg" onclick="m()"/>
    </body>
    </html>
      

  3.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head>
    <script type="text/javascript">
    function m()
    {
    var d = document.getElementById('img1');
    var result=d.src.substr(d.src.lastIndexOf("/")+1);
    alert(result);
    }
    </script>
    <body>
    <img id="img1" src="index.jpg" onclick="m()" />
    </body>
    </html>
      

  4.   

    截取字符串 或者 alert(d.getAttribute('src'));
      

  5.   

    function m()
    {
    var d = document.getElementById('img1');
    var result=d.src.substr(d.src.lastIndexOf("/")+1);
    alert(result);
    }
    就用这个吧!