dsedug8644 2018-06-19 08:06
浏览 84

Javascript返回点击功能不是一个功能

I have two php files - index.php and loadimages.php. The index page has a thumbnail gallery and a canvas. The images to thumbnail gallery is populated through loadimages.php. The following is the snippet of the code from loadimages.php:

for ($i=0; $i<count($files); $i++)
{
    $num = $files[$i];
    $filname = basename($num, ".jpg");
    $filnam = substr($filname, -5);
    echo '<figure class="item">';
    echo '<img class="matchImages" onclick="clickedImage(this.id)" src="'.$num.'" id="thumbNails' . $filnam . '   "/>';
        }

The above php code also has an onclick function clickedImage. In the index.php, i have this function which is as follows:

function clickedImage(clicked_id) {
  localStorage.setItem("clickedImg", clicked_id);
  var clickedImg = document.getElementById(clicked_id).src;
  var clickedImg = clickedImg.replace(/^.*[\\\/]/, '');
  var canvas = document.getElementById("mycanvas");
  var ctx = canvas.getContext("2d");
  var thumbNails = document.getElementById("loaded_img_panel");
  var pic = new Image();
  pic.onload = function() {
    canvas.width = pic.width;
    canvas.height = pic.height;
    ctx.drawImage(pic, 0,0)
  }
  thumbNails.addEventListener('click', function(event) {
    pic.src = event.target.src;
  });
}

The above code works without any errors. I want to build in a functionality where when the page is refreshed, I want to virtually click on the last clicked image so that the user stays on the same image before reload. I have created a eventlistener for this which is as follows:

window.addEventListener('load', function() {
   var clickedImage = localStorage.getItem('clickedImg');
   clickedImage(clickedImage); 
  })

This is not working, and the console returns Uncaught TypeError: clickedImage is not a function.

  • 写回答

2条回答 默认 最新

  • dongtan3395 2018-06-19 08:09
    关注

    Both your function and your variable are named clickedImage:

    var clickedImage = localStorage.getItem('clickedImg');
    clickedImage(clickedImage); 
    

    Rename the variable to something else (e.g. clickedImageValue) and you should be good to go.

    The problem you experience here is called variable shadowing, by the way.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?