[英]pop-up a window when the user clicks a button
這是我的 HTML 代碼:
<p style="align-content: center">
<A style="align-content: center" HREF="newpopup.html" onClick="return popup(this, 'stevie')">my popup</A><br>
</p>
<p align="center">
<input type="button" onclick="popup(this, 'about') " value="CLICK HERE">
</p>
和 JavaScript:
function popup(mylink, windowname)
{
if (! window.focus)
return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=400,height=200,scrollbars=yes');
return false;
}
我的按鈕“單擊此處”會彈出一個窗口,但它是空的。 我希望我的按鈕像上面的 URL 鏈接“我的彈出窗口”一樣工作。
所以我想通過單擊按鈕在彈出窗口中打開newpopup.html
的內容。
我的彈出式 URL 鏈接工作正常,我希望按鈕也能正常工作。
mylink
(按鈕的this
)不是字符串,因此您嘗試打開mylink.href
:
if (typeof(mylink) == 'string')
href = mylink;
else
href = mylink.href;
但是,按鈕沒有href
屬性,所以就好像你寫:
window.open(undefined, 'about', 'width=400,height=200,scrollbars=yes');
正如預期的那樣,它會打開一個空白頁面。 如果要打開與鏈接相同的頁面,請使用:
onclick="popup('newpopup.html', 'about');"
試試這段簡單的代碼:
<script>
function fullwindowpopup(){
window.open("newpopup.html","bfs","fullscreen,scrollbars")
}
</script>
<center>
<form>
<input type="button" onClick="fullwindowpopup()" value="CLICK HERE">
</form>
</center>
如果您希望它們“浮動”在您的 html 頁面上方,彈出窗口可能會很棘手。 這可以通過使用絕對位置在當前頁面上放置“過濾器”和所需框架來獲得。 通常我會做這樣的事情:
HTML
<body>
...
<div class='myPopup'>
<iframe src='http://www.myurl.com' id='popup-frame'>
</iframe> <!-- /#popup-frame -->
</div> <!-- /.myPopup -->
...
</body>
CSS:
.myPopup {
position: absolute;
width: 100%;
height: 100%;
background-color: #A8A8A8;
opacity: 0.4;
display: none;
z-index: 100;
}
#popup-frame {
position: absolute;
top: 50%;
left: 50%;
}
堆棧溢出檢查這個例子。
javascript
在下面
<script>
function openPopup() {
document.getElementById("boxPopup").style.display = "block";
}
function closePopup() {
document.getElementById("boxPopup").style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
var modal = document.getElementById('boxPopup');
if (event.target == modal) {
closePopup();
}
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.