【问题标题】:How to change image through click - javascript [closed]如何通过点击更改图像 - javascript [关闭]
【发布时间】:2012-07-12 05:24:11
【问题描述】:

我有一个包含 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>

【问题讨论】:

  • 我正在尝试替换图像,现在我有 jquery 代码,可让我根据表中的选择替换 div。但是我也想更改表本身,因此当与该表单元格选择DIV时,表单元格看起来有所不同。
  • 以上代码包含无关信息。请将其缩减为仅相关代码,并根据您所做的任何更改对其进行更新。

标签: javascript html css


【解决方案1】:

回答一般问题,您可以使用 javascript 和 CSS 在单击时突出显示单个单元格。我拼凑了一个JS Fiddle example;您可以根据自己的目的进行调整。

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

【讨论】:

  • 我尝试将它应用到我的桌子上,但我无法让它工作。它会干扰我的锚标签。我该如何解决这个问题?
  • 定义干扰。发生了什么样的意外并发症?
  • 好的,我写的代码应该不会导致这种情况,你有自定义修改吗?您需要向我们提供相关代码的最新信息,以便我们为您提供帮助。
  • 最新的代码在上面,但我认为你的代码只适用于文本表格单元格,我的表格单元格是图像。
  • 好的,我没有看到你之前的消息,我会删掉它。我的道歉
猜你喜欢
  • 2014-11-07
  • 1970-01-01
  • 2012-10-24
  • 2020-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多