javascript - 为什么只有一个 JavaScript 函数执行 onClick?

标签 javascript html css dom-events

我是 JavaScript 的新手,据说我在我的代码中遇到了一个奇怪的错误。我的 HTML 中有三张图片,单击它们时应以模式打开。由于某种原因,模式将卡在最后一张新图像上。例如,如果我点击第一个,然后是第三个,然后是第二个,无论我点击第二个显示的是什么图像。我尝试通过将 H1 标签替换为图像名称来进行调试,这会抛出另一条曲线,因为现在需要两次点击才能执行模态。我不确定我在做什么,我们将不胜感激。

JS-

<script>
function mode(param)
{

    smode = param;
    document.getElementById("h1thing").innerHTML = smode;
    document.getElementById("myImage").src = smode;
    run();

}
function run(){
// Get the modal
var modal = document.getElementById('myModal');


// Get the button that opens the modal
var btn = document.getElementById(smode);

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];


// When the user clicks the button, open the modal
btn.onclick = function() {
    modal.style.display = "block";

}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
    smode = "";

}



// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
        smode = "";
    }
}
}
</script>

HTML-

<body>

<h2 id="h1thing">Modal Example</h2>


<!-- Trigger/Open The Modal -->
<center>
<div class="floated_img">
    <input id="ScreenShot.png" type="image" src="ScreenShot.png" width="400" height="300" alt="ScreenShot" onclick="myFunction();mode('ScreenShot.png');">
    </input>
</div>
<div class="floated_img">
    <input id="ScreenShot2.png" type="image" src="ScreenShot2.png" width="400" height="300" alt="ScreenShot2" onclick="myFunction();mode('ScreenShot2.png');">
    </input>
</div>
<div class="floated_img">
    <input id="kimlexi.jpg" type="image" src="kimlexi.jpg" width="400" height="300" alt="Kimlexi" onclick="myFunction();mode('kimlexi.jpg');">
    </input>
</div>
</center>
<!-- The Modal -->
<div id="myModal" class="modal">
  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">

            <img src="x.png" width="40" height="40" alt="X">
    </span>
    <center>
        <img id="myImage" src="" width="600" height="500" alt="Kimlexi">
    </center>
  </div>

</div>
</body>

CSS-

<style>
button {
    background:transparent;
      border:none;
      outline:none;
      display:block;
      height:200px;
      width:200px;
      cursor:pointer;
}
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}


/* Modal Content */
.modal-content {
    background-color: #fefefe;
    margin: auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
}

/* The Close Button */
.close {
    color: #aaaaaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.floated_img
{
    float: left;
}
</style>

最佳答案

问题

这里有两个问题。

  1. 第一次单击按钮不会打开模式。第一次单击图像(让我们以第一张图像为例)时,mode('ScreenShot.png')被执行,其中
    1. 将 smode 设置为 'ScreenShot.png'
    2. <h1> 中显示 smode (='ScreenShot.png')
    3. 将 smode (='ScreenShot.png') 设置为 <img>src
    4. 调用 run(),它改变了 onclick按钮的处理程序,但从未真正打开模态!
  2. 第二次点击同一个按钮可能会显示错误的图像。发生这种情况是因为按钮的 onclick处理程序在上面的步骤 1.4 中被覆盖(它被覆盖,而不是添加),并且新的事件处理程序 ( btn.onclick = function() { modal.style.display = "block"; } ) 仅使模态可见但不更改图像源。

建议的解决方案

如果你只设置 onclick 会更容易处理程序一次。这是一种解决方案。

JS-

function openModal(imgSrc)
{
    document.getElementById("myImage").src = imgSrc;
    document.getElementById("myModal").style.display = "block";
}

function closeModal() {
    document.getElementById('myModal').style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == document.getElementById('myModal')) {
        closeModal();
    }
}

HTML-

<input type="image" src="….png" width="400" height="300" 
        alt="ScreenShot" onclick="openModal('….png');">
…
<span class="close" onclick="closeModal()">

关于javascript - 为什么只有一个 JavaScript 函数执行 onClick?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38029373/

相关文章:

javascript - 简单的 Javascript show div 函数不起作用

javascript - jquery ajax成功时的参数

javascript - 是否可以在不删除该行的情况下淡出文本?

javascript - 如何使用 css 渐变在 jQuery Slider 的两个 handle 之间设置背景

css - 如何将页脚内容居中以适应移动查看

javascript - 具有响应高度的全宽背景

javascript - 表格排序器不适用于 json

html - 在 jsfiddle 上测试 Bootstrap

html - 为什么这些元素的宽度不一样?

jquery - 算法对 Angular 淡入淡出jquery