如何通过点击更改图像 - JavaScript

时间:2012-07-11 14:32:17

标签: javascript html css

我有一个包含5个表格单元格的工具栏。第一个细胞看起来很清晰,另外4个细胞有阴影。我想这样做,以便点击表格单元格也会改变图像,使阴影也会相对于所选的当前表格单元格发生变化。

<script src="../../jquery-1.7.2.min.js" type="text/javascript"> 

    $('table.stories-preview td').click((function () {
    var last_image = null;

    return function () {
        if (last_image) {
            $('table.stories-preview .selected').attr('src', last_image).removeClass('selected');
        }

        var $clicked_image = $('img', this);
        last_image = $clicked_image.attr('src');
        $clicked_image.attr('src', 'http://i1251.photobucket.com/albums/hh544/kochb/th_checklist_on.png').addClass('selected');
    }
})());


     $(document).ready(function() {
    $(".fade").css("display", "none");

    $(".fade").fadeIn(500);



    function redirectPage() {
        window.location = linkLocation;
    }
}); 

    $(document).ready(function() {

     $('.preview').hide();

  $('#link_1').click(function(){ 
      $('#latest_story_preview1').hide();
       $('#latest_story_preview2').hide();
      $('#latest_story_preview3').hide();
      $('#latest_story_preview4').hide();
      $('#latest_story_main').fadeIn(800);
  });   

  $('#link_2').click(function(){ 
      $('#latest_story_main').hide();
      $('#latest_story_preview2').hide();
      $('#latest_story_preview3').hide();
      $('#latest_story_preview4').hide();
      $('#latest_story_preview1').fadeIn(800);
  });

  $('#link_3').click(function(){ 
      $('#latest_story_main').hide();
      $('#latest_story_preview1').hide();
      $('#latest_story_preview3').hide();
      $('#latest_story_preview4').hide();
      $('#latest_story_preview2').fadeIn(800);
  });

    $('#link_4').click(function(){ 
      $('#latest_story_main').hide();
       $('#latest_story_preview1').hide();
      $('#latest_story_preview2').hide();
      $('#latest_story_preview4').hide();
      $('#latest_story_preview3').fadeIn(800);
  });
    $('#link_5').click(function(){ 
      $('#latest_story_main').hide();
       $('#latest_story_preview1').hide();
      $('#latest_story_preview2').hide();
      $('#latest_story_preview3').hide();
      $('#latest_story_preview4').fadeIn(800);
  });





    });

</script>




    <table class="stories-preview" width="330" height="598" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            <a id="link_1" href="#"> <img src="../../Images/N&EImages/images-con-1/Article-Nav-Bar1_01.gif" width="330" height="114" alt=""></a></td>
    </tr>
    <tr>
        <td>
            <a id="link_2" href="#"> <img src="../../Images/N&EImages/images-con-1/Article-Nav-Bar1_02.gif" width="330" height="109" alt=""> </a></td>
    </tr>
    <tr>
        <td>
        <a id="link_3" href="#"> <img src="../../Images/N&EImages/images-con-1/Article-Nav-Bar1_03.gif" width="330" height="132" alt=""></a></td>
    </tr>
    <tr>
        <td>
            <a id="link_4" href="#"><img src="../../Images/N&EImages/images-con-1/Article-Nav-Bar1_04.gif" width="330" height="124" alt=""></a></td>
    </tr>
    <tr>
        <td>
            <a id="link_5" href="#"><img src="../../Images/N&EImages/images-con-1/Article-Nav-Bar1_05.gif" width="330" height="119" alt=""></a></td>
    </tr>
</table>

1 个答案:

答案 0 :(得分:0)

回答一般问题,您可以使用javascript和CSS突出显示单击的单个单元格。我把JS Fiddle example扔了起来;你可以根据自己的需要进行调整。

这是a version that can swap out images给你的。