我在制作一个帖子样式的提要对话框时遇到了这个问题,我会点击“心”,它会显示一个喜欢它的人的 iframe。弹出对话框将在用户单击图片时显示,它会给他们一个弹出窗口,其中包含指向该图片的个人资料页面的链接以及指向消息的链接。我将一个变量传递给父 iframe 以使链接正常工作。这是我使用的弹出示例:https ://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_popup
在我的外部 iframe 中,我将弹出窗口放在正在渲染图片的 iframe 旁边。然后我使用java来触发它。我唯一要做的就是将我留在喜爱的 post iframe 中的 javascript 部分更改为弹出容器的新位置。java 调用在 mysql 结果循环阶段呈现。
<?php
if ($count>0){
echo '<div style="max-width:10em;"><table border=1 style=" border-color:#696969;">';
while ($data = $result->fetch_row()) {
echo '<tr>';
for ($m=0; $m<$result->field_count; $m++) {
if ($m=="0"){
$picSrc= $data[$m];
}else if ($m=="1"){
$usrName=$data[$m];
}else if ($m=="2"){
$userRec= $data[$m];
}
}
echo '<td style="background-color:#eac3a8;"><img src="'.$picSrc.'" onclick="myFunction('.$userRec.','.$usrName.')"> <br>';
echo '</tr>';
}
echo '</table></div>';
$result->close();
} else {
echo "No one has loved this posted";
}
// since I am rendering dynamically, I need to encapsulate the JavaScript into php, and pass the link html to the popup.
//$pstId is my id for the posting in the feed
echo '<script>
function myFunction(a,b) {
var userRec=a;
var usrName=b;
var links=" <a href=\'visitprofile.php?usr="+userRec+"\' target=\'_blank\'>Visit "+ usrName + "\'s Profile</a> <br> <a href=\'messagethem.php?usr="+userRec+"\' target=\'_blank\'>Send "+ usrName +"a message </a>";
var popup = parent.document.getElementById("'.$pstId.'");
popup.innerHTML=links;
popup.classList.toggle("show");
}
</script>';
这是在父 iframe 中:
<style>
/* Popup container - can be anything you want */
.popup {
position: relative;
display: inline-block;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* The actual popup */
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -80px;
}
/* Popup arrow */
.popup .popuptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
/* Toggle this class - hide and show the popup */
.popup .show {
visibility: visible;
}
</style>
// then the span id is dynamically generated.
<div class="popup"><span class="popuptext" id="myPopupxs43vbty"></span></div>