世界杯,熬夜。球看完了,睡之前,再提个问。
有一个链接,默认为:
<div id="link">
<a href="1.html" target="_self"><img src="1.jpg"/></a>
</div>
如何用JS定义:
当img src="2.jpg"时,a href="2.html"
当img src="3.jpg"时,a href="3.html"
当img src="4.jpg"时,a href="4.html"
……

解决方案 »

  1.   

    为了让你看明白我分步写了下
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>    <script type="text/javascript">
        window.onload=function()
        {
            var url = document.getElementById("link").childNodes[0].childNodes[0].src;//是以个完整的路径 然后开始截取
            url = url.substring(url.lastIndexOf('/')+1,url.length);//1.jpg
            var ints = url.substring(0,url.length-4);//1
            
            var newUrl = ints+".html";//1.html
            document.getElementById("link").childNodes[0].href=newUrl;//赋值该路径
         
        }
        </script>    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
        <title>我的JavaScript之旅!</title>
    </head>
    <body>
        <div id="link">
            <a href="1.html" target="_self">
                <img src="2.jpg" /></a>
        </div>
    </body>
    </html>
      

  2.   

    老虎兄:感谢你的教授。
    但是貌似从1加载到N以后,不能返回去。
    能否再根据需要的返回前面的数值?
    比如:加载到img src="5.jpg",a href="5.html"时,可以再返回前面的 img src="2.jpg",a href="2.html"?
      

  3.   

    补充,JQUERY加载IMG不刷页,所有window.onload貌似只能使用一次……
      

  4.   

    onload事件只能触发一次,但是onload里面要执行的函数或功能语句却可以调用多次吧
      

  5.   


    返回前面的数值? 没搞明白这句话!!你说的返回 是什么意思呢?我感觉你说的是 你的页面上 有好多超链接 都需要做这样的操作??
    <a href="1.html" target="_self"><img src="1.jpg" /></a>
    <a href="1.html" target="_self"><img src="2.jpg" /></a>
    <a href="1.html" target="_self"><img src="3.jpg" /></a>
      

  6.   

    <a href="#" onclick="func(this)" target="_self"><img src="X.jpg" /></a>
    <script type="text/javascript">
    function func(obj){
      var src = obj.childNodes[0].src;
      obj.href=src.replace(".jpg",".html");
    }
    </script>
      

  7.   

    那你img的src要是动态的数据源就行了,根据你img的src来创建链接href的值