How to find with javascript the current page in a <ul> tree
I am trying to create dynamic next and previous buttons with javascript. I am stuck at the point of trying to create a variable that describes the current page I am on, relative to a ul tree.
I have looked at
window.location;
But after returning the full url, I am stuck at striping this string down to just the x.html and then finding this in the li links.
After finding an li link in my ul tree I can generate the next and previous links using children as in this fiddle: http://jsfiddle.net/JyfS2/
Example code 开发者_开发百科- If I am on page b, find this page, and make the PREV link link to a.html, and the NEXT to c.html
<ul>
<li id="1"><a href="a.html">a</a></li>
<li id="2"><a href="b.html/">b</a></li>
<li id="3"><a href="c.html">c</a></li>
</ul>
<div>
<a id="next" href="">next</a>
<a id="prev" href="">prev</a>
</div>
Some JS I have been using - So I would want to replace $('li#2') with a variable that describes the current page:
<script>
var next_url = $('li#2').next()[0].firstChild.href;
var prev_url = $('li#2').prev()[0].firstChild.href;
var loc = window.location;
alert(loc);
document.getElementById('next').href = next_url;
document.getElementById('prev').href = prev_url;
</script>
This will find the page from a url:
var url = 'http://www.test.com/index.html'
var split = url.split('/');
var page = split[split.length-1];
alert(page);
精彩评论