【javascript权威指南】images_click

点击图片的时候将图片隐藏

<!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=utf-8" />
<title>images_onclick</title>
</head>

<body>
<div id='photo'>
    <img src="images/1.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/3.jpg" alt="" />
</div>
<script type="text/javascript">
 window.onload = function(){
     var images = document.getElementsByTagName('img');
    for(var i=0;i<images.length;i++){
        var cur_img = images[i];
        if(cur_img.addEventListener){
            cur_img.addEventListener('click',hide,false);
        }else{
            cur_img.attachEvent('onclick',hide);
        }
    }
    
    function hide(event){
        getTarget = event.target || event.srcElement;  //ie8及之前要使用event.srcElement
        getTarget.style.visibility = 'hidden';
    }
 }
</script>
</body>
</html>

 

posted @ 2014-05-19 17:01  Western Journey  阅读(124)  评论(0编辑  收藏  举报