<div id="sample_bigimg"><img src="aubergine.jpg" width="120px" height="120px;" /></div>
<div><img src="palomino.jpg" width="50px" height="50px" id="sample_smallimg1" onclick="update()" /></div>
<div><img src="cobalt.jpg" width="50px" height="50px" id="sample_smallimg2" onclick="update()" /></div>我想实现点击小图的时候大图能换成对应的小图,js如下:
<script>
function update(){
var image = $("#sample_smallimg1").attr("src"); 
$("#sample_bigimg img").attr("src",image);
}
</script>
这串代码切换没有问题
但是为了实现每个小图的切换,我需要能够先获取到id值,于是我用下面的代码实现
var ids  = $(this).attr('id');
alert(ids);
但是这边alert为undefined,怎么改?麻烦大家了

解决方案 »

  1.   


    <script src="http://ajax.Microsoft.com/ajax/jQuery/jquery-1.3.2.min.js" type="text/javascript"> </script> 
    <script>
    $().ready(function(){
    for(var i=1;i<3;i++){
    $("#sample_smallimg" + i).click(
    function(event){
    var image = $(event.target).attr("src");
    alert(image);
    $("#sample_bigimg img").attr("src",image);
    }
    );
    }
    });
    </script>
    <div id="sample_bigimg"> <img src="aubergine.jpg" width="120px" height="120px;" /> </div> 
    <div> <img src="palomino.jpg" width="50px" height="50px" id="sample_smallimg1" /> </div> 
      

  2.   

    少贴一行,补一下<script src="http://ajax.Microsoft.com/ajax/jQuery/jquery-1.3.2.min.js" type="text/javascript"> </script> 
    <script>
    $().ready(function(){
    for(var i=1;i<3;i++){
    $("#sample_smallimg" + i).click(
    function(event){
    var image = $(event.target).attr("src");
    alert(image);
    $("#sample_bigimg img").attr("src",image);
    }
    );
    }
    });
    </script>
    <div id="sample_bigimg"> <img src="aubergine.jpg" width="120px" height="120px;" /> </div> 
    <div> <img src="palomino.jpg" width="50px" height="50px" id="sample_smallimg1" /> </div> 
    <div> <img src="cobalt.jpg" width="50px" height="50px" id="sample_smallimg2" /> </div>
      

  3.   

    最简单的方法,在所有小图外面加个div
    <div id="small-images">
    <div><image .... id="small1"/></div>
    <div><image .... id="small2"/></div>
    <div><image .... id="small3"/></div>
    </div>
    js
    var ids = "";
    $("#small-images").find("image").each(function(){
    ids += $(this).attr("id");
    });
      

  4.   

    如果下面每个再加一行字,比如这个
    <div id="sample_smallimg1_text">palomino</div>
    <div id="sample_smallimg2_text">cobalt</div>
    那代码里应该加什么哦?