jQuery when pointed to a link should show a div that's hidden by default

旧街凉风 提交于 2019-12-07 19:29:50

问题


I have a page where there are a few divs hidden by default. I would like to be able to point users to a link where it would show the divs.

ex. https://app.emailsmsmarketing.com/login

Users are able to click "Register" which hides the login div and shows the register div. What I'm trying to accomplish is basically adding a link to the main site from where users will be able to access the registration form by default (using jQuery only).

ex. https://app.emailsmsmarketing.com/login#!register (or something like that)

Basically what I'm asking is:

a) is is possible to do this
b) if so, how?

I'm not sure if this makes sense to anyone. I appreciate any help provided.


回答1:


You probably looking for this: Anchor-based URL navigation with jQuery

var myUrl = document.location.toString();
if (myUrl.match('#')) { // the URL contains an anchor

  var myAnchor = '#' + myUrl.split('#')[1];
  $('#login').hide();
  $('#register').show();
}



回答2:


You can examine the document.location property during ready event:

$(document).ready(function() {
  if (document.location.indexOf('#login') > -1)
    $("#login").show();
});



回答3:


Well sure, just set a class or id or something on your link, like so:

<a href="#" class="register"> Register! </a>

Then do this in jQuery

$("a.register").click(function() { 
    $("#logindiv").hide()
    $("#registerdiv").show();
    return false; // prevents the default behavior of the link, ie following it
});

Where registerdiv is the ID of your hidden div etc.



来源:https://stackoverflow.com/questions/7329585/jquery-when-pointed-to-a-link-should-show-a-div-thats-hidden-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!