簡體   English   中英

在css中打開模態窗口時運行javascript函數

[英]Running a javascript function while opening modal window in css

單擊錨標記時,我可以打開模態窗口。 我正在運行的onClick函數似乎沒有被調用。 我希望能夠彈出模態窗口並通過Javascript向其添加圖像。 以下是適用的代碼:

HTML

<a href="#openModal" class="submitBtn" onclick="return displayPhoto();">Submit</a>

<div id="openModal" class="modalDialog">
    <div>
        <table>
            <tr height="500px">
                <td id="photo"></td>
                <td id="description">
                </td>
            </tr>
        </table>
    </div>
</div>  

CSS

.modalDialog {
    position: fixed;
    font-family: Arial, Helvetica, sans-serif;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0,0,0,0.8);
    z-index: 99999;
    opacity:0;
    -webkit-transition: opacity 400ms ease-in;
    -moz-transition: opacity 400ms ease-in;
    transition: opacity 400ms ease-in;
    pointer-events: none;
}

.modalDialog:target {
    opacity:1;
    pointer-events: auto;
}

.modalDialog > div {
    width: 600px;
    position: relative;
    margin: 5% auto;
    padding: 5px 20px 13px 20px;
    border-radius: 10px;
    background: #fff;
    background: -moz-linear-gradient(#fff, #999);
    background: -webkit-linear-gradient(#fff, #999);
    background: -o-linear-gradient(#fff, #999);
}

#photo{
    width:auto;
}

使用Javascript

function displayPhoto(){
    window.alert(11);
    document.getElementById('photo').innerHTML = "<img src="images/characters/gandalf.jpg" />";
    return true;
}

你需要在innerHTML轉義引號

document.getElementById('photo').innerHTML = "<img src=\"images/characters/gandalf.jpg\" />";

這是一個JSFiddle。 正如@Maverick指出的那樣,你的getElementById行有一個字符串錯誤。

http://jsfiddle.net/tz1att9j/5/

function displayPhoto(){
    alert(11);
    document.getElementById('photo').innerHTML = "<img src='images/characters/gandalf.jpg' />";
    return true;
}

暫無
暫無

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

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