anchor

How can you check for a #hash in a URL using JavaScript?

天涯浪子 提交于 2019-11-25 22:28:30
问题 I have some jQuery/JavaScript code that I want to run only when there is a hash ( # ) anchor link in a URL. How can you check for this character using JavaScript? I need a simple catch-all test that would detect URLs like these: example.com/page.html#anchor example.com/page.html#anotheranchor Basically something along the lines of: if (thereIsAHashInTheUrl) { do this; } else { do this; } If anyone could point me in the right direction, that would be much appreciated. 回答1: Simple: if(window

offsetting an html anchor to adjust for fixed header [duplicate]

我的梦境 提交于 2019-11-25 22:23:44
问题 This question already has answers here : Fixed page header overlaps in-page anchors (32 answers) Closed 4 years ago . I am trying to clean up the way my anchors work. I have a header that is fixed to the top of the page, so when you link to an anchor elsewhere in the page, the page jumps so the anchor is at the top of the page, leaving the content behind the fixed header (I hope that makes sense). I need a way to offset the anchor by the 25px from the height of the header. I would prefer HTML

Smooth scrolling when clicking an anchor link

自作多情 提交于 2019-11-25 21:55:34
问题 I have a couple of hyperlinks on my page. A FAQ that users will read when they visit my help section. Using Anchor links, I can make the page scroll towards the anchor and guide the users there. Is there a way to make that scrolling smooth? But notice that he\'s using a custom JavaScript library. Maybe jQuery offers somethings like this baked in? 回答1: Update April 2018: There's now a native way to do this: document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener(

How to create an HTML button that acts like a link?

大憨熊 提交于 2019-11-25 21:40:04
问题 I would like to create an HTML button that acts like a link. So, when you click the button, it redirects to a page. I would like it to be as accessible as possible. I would also like it so there aren\'t any extra characters, or parameters in the URL. How can I achieve this? Based on the answers posted so far, I am currently doing this: <form method=\"get\" action=\"/page2\"> <button type=\"submit\">Continue</button> </form> but the problem with this is that in Safari and Internet Explorer, it

How to enable or disable an anchor using jQuery?

旧城冷巷雨未停 提交于 2019-11-25 20:55:48
How to enable or disable an anchor using jQuery? karim79 To prevent an anchor from following the specified href , I would suggest using preventDefault() : // jQuery 1.7+ $(function () { $('a.something').on("click", function (e) { e.preventDefault(); }); }); // jQuery < 1.7 $(function () { $('a.something').click(function (e) { e.preventDefault(); }); // or $('a.something').bind("click", function (e) { e.preventDefault(); }); }); See: http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29 Also see this previous question on SO: jQuery disable a link The app I'm currently working on