将链接弹出窗口更改为“加载屏幕时打开”弹出操作

时间:2015-09-09 16:20:41

标签: javascript html

我需要使用以下代码,这些代码最初用于在用户单击图像时打开弹出消息,并在网页完成加载时将其更改为打开。如何保持相同的基本代码格式(即头部没有< script>?)

我尝试将onclick更改为onload,此解决方案无效。

   <a href="javascript:void(0)" onclick="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'"> 
       <img alt="" src="/Portals/0/Images/Banner/banner-mobile-cafeteria.jpg" class="banner-img" />
</a> 
<div id="light" class="white_content">
   please click 
    <a href="http://midwestfolding.artcobell.com" target="_blank">here</a>.
 (Note, by clicking the link you will be leaving the Midwest Folding Products website. <br /> <br />
 To return, please click <a href="javascript:void(0)" onclick="document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'">here</a>.
</div> <div id="fade" class="black_overlay"></div>

2 个答案:

答案 0 :(得分:1)

首先,在页面加载后显示元素对我来说没什么意义。你可以改为想要显示display:block的div的css而忘记任何javascript。但那不是问题所在......

如果我理解正确,您希望在加载页面时打开弹出窗口:

使用Javascript:

这将触发在加载页面后启动的功能(并打开弹出窗口):

<script>
window.onload = function () { 
   window.open ("http://www.yourlink.com","title");
}

</script>

或者如果您希望改变它的可见性:

<script>
window.onload = function () { 
   document.getElementById("#yourElement").style.display = 'block';
}
</script>

使用jquery,您可以在页面加载时打开弹出窗口,如下所示:

<script>
$( document ).ready(function() {
     $(function() {
      window.open ("http://www.yourlink.com","title");
    });
});
</script>

然后你评论说你希望在页面加载后打开一个对话框,这不是你原来的问题,但是w.e:

支票jquery dialog。设置模态为true会使背后的屏幕变暗。你需要有jquery库(google it)。

<div id="dialog-message" title="Important information">
    hi
</div>

<script>
$("#dialog-message").dialog({
    modal: true,
    draggable: false,
    resizable: false,
    position: ['center', 'top'],
    show: 'blind',
    hide: 'blind',
    width: 400,
    dialogClass: 'ui-dialog-osx',
    buttons: {
        "YES": function() {
            $(this).dialog("close");
        }
    }
});
</script>

答案 1 :(得分:0)

您可以将事件处理程序移动到body标记:

<body onload="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">

锚点do not have onload events,所以你不能把它留在那里。