javascript - 在 css 中打开模态窗口时运行 javascript 函数

标签 javascript html css

我可以在单击 anchor 标记时打开模式窗口。但是,我正在运行的 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\" />";

关于javascript - 在 css 中打开模态窗口时运行 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27086108/

相关文章:

javascript - 将一个事件处理程序分配给多个元素?

html - CSS全局链接后台问题

javascript - babel-loader 不转换 webpack.config.js

javascript - 使用 nodejs 的 Forever 将 console.logs 输出到屏幕

jquery - 导航栏超出页面宽度

javascript - mapbox/leaflet js map 上的粗白线

html - 菜单一直是 "clicked"。悬停效果出错

javascript - 在 ng-repeat 中显示带空格的键名

JavaScript 类型错误 : cannot read property 'split' of undefined

html - 你能在html中添加背景图片吗?