0

这是基本的 HTML 标记。

<a id="open" href="#popup">click</a>
<div id="popup">content</div>

我已经<div id="popup">默认隐藏了,点击<a id="open">打开<div id="popup">

如果用户输入带有井号标签的 URL,我可以让它默认打开#popupexample.com/#popup

4

1 回答 1

3

display: none;在你的 CSS 中使用 a :

#popup {
    display: none;
}

然后在您的 JS 中使用其中任何一种:

$("#open").on('click', function(e) {
    e.preventDefault();
    $("#popup").toggle(); //When clicked, toggle visibility.
});

$(window).on('hashchange', function() {
    //You can detect a hash change like this
    //Since your href is set to #popup,
    //you can put the .toggle() in here as the hash will change when clicked.
    console.log("yolo");
});

if(window.location.hash == "#popup") {
    //If it is initialized with the hash #popup (ie. example.com#popup and Enter)
    //Use this
    console.log("yolo2");
    $("#popup").show();
}

大提琴_

于 2013-03-27T14:54:56.893 回答