简体   繁体   English

如何使用javascript查找当前页面 <ul> 树

[英]How to find with javascript the current page in a <ul> tree

I am trying to create dynamic next and previous buttons with javascript. 我正在尝试使用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. 我被困在尝试创建一个变量来描述相对于ul树的当前页面。

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. 但是,在返回完整的URL之后,我被困在将字符串简化为x.html的位置,然后在li链接中找到它。

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/ 在我的ul树中找到li链接之后,我可以像下面的小提琴一样使用子对象生成下一个和上一个链接: 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 示例代码-如果我在页面b上,请找到此页面,然后将PREV链接链接到a.html,将NEXT链接到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: 我一直在使用一些JS-因此,我想用描述当前页面的变量替换$('li#2'):

    <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: 这将从URL中找到页面:

var url = 'http://www.test.com/index.html'

var split = url.split('/');
var page = split[split.length-1];

alert(page);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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