簡體   English   中英

為什么只有一個JavaScript函數執行onClick?

[英]Why does only one JavaScript function execute onClick?

我對JavaScript有點陌生,也就是說我在代碼中遇到了一個奇怪的錯誤。 我的HTML中有3張圖片,當單擊它們時應以模式打開。 由於某種原因,模態將卡在新圖像的最后一個圖像上。 例如,如果我單擊第一個,然后單擊第三個,然后單擊第二個,無論我在第二個顯示器上單擊什么圖像。 我嘗試通過將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()">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM