zoukankan      html  css  js  c++  java
  • 关于点击a链接就会显示指定图片

     1 <ul id="image">
     2         <li><a href="Text_img/1.jpg" title="1">1</a></li>
     3         <li><a href="Text_img/2.jpg" title="2">2</a></li>
     4         <li><a href="Text_img/3.jpg" title="3">3</a></li>
     5         <li><a href="Text_img/4.jpg" title="4">4</a></li>
     6     </ul>
     7     <img src="Text_img/5.jpg" alt="" id="place">
     8     <p id="text">choose picture</p>
     9     <script>
    10         function pre(){
    11             var imge=document.getElementById("image");
    12             var link=imge.getElementsByTagName("a");
    13             for(var i=0;i<link.length;i++){
    14                 link[i].onclick=function(){
    15                     showPic(this);
    16                     console.log(i);     //4
    17                     console.log(this);  //选中指定图片
    18                     return false;
    19                 }
    20             }
    21         }
    22         window.onload=pre;
    23     </script>
    24 
    25     <script>
    26         function showPic(Img){
    27             var souce=Img.getAttribute("href");
    28             var place=document.getElementById("place");
    29             place.setAttribute("src",souce);
    30             // var li=object.getAttribute("class");
    31             // console.log(li);     //显示不出来??
    32             var title=Img.getAttribute("title");
    33             var text=document.getElementById("text");
    34             text.childNodes[0].nodeValue=title;
    35         }

    return false  当showPic()正常返回,pre()函数取消onclick事件的默认行为(不添加,点击a会直接跳转新页面)

    其中一些问题:this指向

              li[i]中i一直为4,为什么this还能指到指定的图片

  • 相关阅读:
    Hadoop 2.5.1集群安装配置
    Hadoop 2.5.1编译
    CloudStack安装
    Swift安装
    频率分布折线图与总体密度曲线
    频率直方图(hist)
    分位数(quantile)
    茎叶图(stem)
    盒图(boxplot)
    R语言学习
  • 原文地址:https://www.cnblogs.com/icola/p/10954564.html
Copyright © 2011-2022 走看看