17

I was trying to make a div jump from <a href="#FAQ">FAQ</a> to a div id and could success easily, but since I have a fixed link to jump to, I can't seem to find a way how to do that.

Meaning, I want a div or a href link to jump to a url in the same page say <a href="http://www.mydomain.ask.html">Ask</a>

I know for sure that jumping from an anchor such as <a href="#jump">jump</a> to a div such as <div id="jump">jump to point</div> works fine. However, how about the opposite way? does it work? I mean jumping from div id to an anchor works?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Digital site
  • 4,431
  • 12
  • 48
  • 72

3 Answers3

30

As long as your anchor has an ID you can link to a location in your view by clicking the div you will just need some javascript.

Setting the window.location.hash value will put the focus of the view on the selected ID.

Sample

<a href="#test1" id="back">Test</a>

<div id="test1" onclick="window.location.hash='back'; ">

JSFiddle Example

Malkus
  • 3,686
  • 2
  • 24
  • 39
  • ok, great solution. This is what I did,too. However, what I want to do is jumping from the `div id="test1"` to `a href="#test1"`, not the normal way we know... – Digital site Jul 23 '13 at 15:18
  • just found out in your example that I have to click **test** to jump to the **target div** and then click the **target div** to go back to **test** anchor, which is good. however, I want only a jump from **target div** to **test** "one way" – Digital site Jul 23 '13 at 15:29
  • 1
    You do not _have_ to use the exact example I provided. – Malkus Jul 23 '13 at 15:33
  • 1
    In regards to not using the **ID** to set the focus you still need to find the element in the DOM to set the focus. Using an element is the fastest way to search the DOM other searches (such as finding anchor tags with a specific href value) would impact performance. – Malkus Jul 23 '13 at 15:35
  • ok, great to know. Thanks a lot. I will keep digging finding the best possible way without affecting the performance. Thanks – Digital site Jul 23 '13 at 15:38
4

I think then you have to add <a href="yourid">page place</a> inside your first div and have to give id to your anchor tag <a href="yourplace" id="idname"></a>

Mike Phils
  • 3,475
  • 5
  • 24
  • 45
4

if you have link to go another with variable you can use with id # sign

<a href="product.php?action=edit#lastdiv>Go to Product Last Div</a>

<div id="lastdiv">There are last product Exist</div>
Ye Htun Z
  • 2,079
  • 4
  • 20
  • 31