0

我知道有更有效的方法可以做到这一点,但我有这样做的理由。我有一个模态弹出窗口。我希望访问者加载页面后立即弹出此窗口。到目前为止,通过单击将它们带到 index.php#login_form 的链接可以到达并打开该窗口。

“#login_form”是我想在页面加载时添加 URL。然后他们可以选择在它最初加载弹出窗口后退出它。

现在有没有办法更有效地做到这一点而不必改变我的 css 或代码?

谢谢!

4

3 回答 3

2

可以通过window.location.hashjavascript 访问 url 中的哈希值。您可以在 body onload 事件中判断这一点。

于 2013-11-21T04:32:05.753 回答
1

为了回答你的问题,我创建了一个小提琴,它以你的例子并解决你正在寻找的东西。http://jsfiddle.net/sgaurav/xA4vG/

基本上这段代码正在做的是,选择你想要模拟的点击的 id,然后根据这里给出的答案创建一个鼠标事件点击如何模拟用户点击 JQuery 中的链接?

$.fn.simulateClick = function() {
    return this.each(function() {
        if('createEvent' in document) {
            var doc = this.ownerDocument,
                evt = doc.createEvent('MouseEvents');
            evt.initMouseEvent('click', true, true, doc.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
            this.dispatchEvent(evt);
        } else {
            this.click(); // IE
        }
    });
}

现在,此代码用于 body 的 onload 事件来伪造点击您手动执行的链接,直到现在使用

jQuery(document).load(
    jQuery('#join_pop').simulateClick()
);

这反过来会在页面打开后立即加载弹出窗口。如果需要,您可以将最后代码中的 id 更改为登录表单,这将开始显示在页面加载而不是注册时。

于 2013-11-21T04:37:51.693 回答
0

一种简单的方法是直接使用主题标签 login_form 加载页面:

http://www.script-tutorials.com/demos/222/index.html#login_form

或者,如果您想更“精确”,您可以像这样使用 jquery:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<body>

<!--Place this at the end of the body tag.-->
<script>
$(function(){
   window.location.hash = "login_form"; //this will add with js the #login_form hash at the end of th url
})
</script>


您可以使用 jquery 在加载窗口时显示模态: 试试这个代码,你会明白:

$(function(){
   alert("Done loading");   
})

您将添加代码以显示模式而不是警报功能。如果模态框使用 css 显示或隐藏,您可以轻松地将 css 类添加到元素中:

$(".element").addClass("showModal);

或删除一个类:

 $(".element").removeClass("hideModal");

确保已导入 jquery 库。我希望这回答了你的问题。

于 2013-11-21T04:23:47.487 回答