【问题标题】:pop-up a window when the user clicks a button当用户单击按钮时弹出一个窗口
【发布时间】:2015-08-21 18:50:57
【问题描述】:

这是我的 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 链接工作正常,我希望按钮也能正常工作。

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    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');"
    

    【讨论】:

      【解决方案2】:

      试试这段简单的代码:

      <script>
         function fullwindowpopup(){
            window.open("newpopup.html","bfs","fullscreen,scrollbars")
         }
      </script>
      

      <center>
          <form>
              <input type="button" onClick="fullwindowpopup()" value="CLICK HERE">
          </form>
      </center>
      

      【讨论】:

      • 这里的第二个参数“bfs”是什么意思?
      【解决方案3】:

      如果您希望弹出窗口“浮动”在您的 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%;
      }
      

      【讨论】:

      • 我不想要一个弹出窗口,我想要右侧带有十字按钮的弹出窗口,右侧的滚动条和全屏。所以我选择了弹出窗口,但不知道如何在按钮中实现这个
      • @SidJj 我不是来为你编码的。提供的代码将为您提供构建弹出窗口的基本框架。
      【解决方案4】:

      Stack Overflow 检查这个例子。

      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>
      

      【讨论】:

        猜你喜欢
        • 2021-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多