简体   繁体   中英

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

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.

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

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

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();
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM