我可以在单击 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/