简体   繁体   English

onclick函数仅在第二次单击后才起作用

[英]onclick function it's working only after second click

I have (onclick) function code for image view that code is working perfectly the issue is it is working when i click second time only. 我有(onclick)用于图像视图的功能代码,该代码运行正常,问题是当我仅第二次单击时它正在运行。 I can't understand what is the issue is please help me to fix this issue. 我不明白问题是什么,请帮助我解决此问题。 I attached the code below.. 我在下面附上了代码。

<script type="text/javascript">

        function showimage(z)
        {
                var modal = document.getElementById('myModal'+z);
                var img = document.getElementById('myImg'+z);
                var modalImg = document.getElementById('img01'+z);
                var captionText = document.getElementById('caption'+z);

                img.onclick = function ()
                {
                    modal.style.display = "block";
                    modalImg.src = this.src;
                    captionText.innerHTML = this.alt;
                }
        }
    </script>

<span>
      <img src="<?php echo $u_pimg; ?>" alt="" class="newsize" id="myImg<?php echo $p_id; ?>" onclick="showimage(<?php echo $p_id; ?>)">
    enter code here
</span>

First ur binding the "showimage" method to be called when u click on the image then ur binding 首先,当您单击图像时,便会绑定要在单击图像时调用的“ showimage”方法

function ()
                {
                    modal.style.display = "block";
                    modalImg.src = this.src;
                    captionText.innerHTML = this.alt;
                }

on click of the image. 点击图片。 So u should not bind the event again on the image tag. 因此,您不应将事件再次绑定在image标签上。

remove either onclick="showimage()" from ur html tag or remove img.onclick from the JS code. 从html标记中删除onclick =“ showimage()”或从JS代码中删除img.onclick。

write it like : 像这样写:

function showimage(z)
        {
                var modal = document.getElementById('myModal'+z);
                var img = document.getElementById('myImg'+z);
                var modalImg = document.getElementById('img01'+z);
                var captionText = document.getElementById('caption'+z);

                 modal.style.display = "block";
                    modalImg.src = img.src;
                    captionText.innerHTML = this.alt;
        }

and it will work perfectly. 它会完美地工作。

It is not clear what your code is doing, 'cause you provide only small piece of it. 目前尚不清楚您的代码在做什么,因为您只提供了其中的一小部分。 But you definitely have a completely unnecessary onlick inside your showimage function. 但是,您在showimage函数中绝对没有必要使用onlick Try to remove the inner img.onclick perhaps? 尝试删除内部img.onclick吗?

     <script type="text/javascript">

        function showimage(z)
        {
                var modal = document.getElementById('myModal'+z);
                var img = document.getElementById('myImg'+z);
                var modalImg = document.getElementById('img01'+z);
                var captionText = document.getElementById('caption'+z);

                modal.style.display = "block";
                modalImg.src = img.src; // <- replace this with img
                captionText.innerHTML = img.alt;
        }
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM