<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>风牛菜籽 &#187; bbpress</title>
	<atom:link href="http://www.caiyanpei.com/tag/bbpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.caiyanpei.com</link>
	<description>拖延症重症患者</description>
	<lastBuildDate>Tue, 25 Mar 2025 01:51:19 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.6.1</generator>
		<item>
		<title>js处理导航菜单当前位置高亮</title>
		<link>http://www.caiyanpei.com/js-nav-highlight/</link>
		<comments>http://www.caiyanpei.com/js-nav-highlight/#comments</comments>
		<pubDate>Sun, 12 Sep 2010 15:01:40 +0000</pubDate>
		<dc:creator>tsai</dc:creator>
				<category><![CDATA[技术]]></category>
		<category><![CDATA[bbpress]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://cyp.me/?p=246</guid>
		<description><![CDATA[在wordpress中，当用Category或Page当导航菜单的话，wordp &#8230; <a href="http://www.caiyanpei.com/js-nav-highlight/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>在wordpress中，当用Category或Page当导航菜单的话，wordpress已经处理好当前li标签的class了，所以用wordpress的Category或Page当导航菜单不用作本文的处理。我在bbpress中，想把每个父版块做成导航菜单的时候碰到这个问题的。</p>
<p>Jeremy Keith 在《JavaScript DOM编程艺术》一书中为我们做了一个小的例子，例子中使用了一段简单的高亮当前位置的 js 代码，代码如下：</p>
<pre class="brush: jscript; title: ; notranslate">
function highlightPage() {
       if (!document.getElementsByTagName) return false;
       if (!document.getElementById) return false;
       if (!document.getElementById(&quot;navigation&quot;)) return false;

       var nav = document.getElementById(&quot;navigation&quot;);
       var links = nav.getElementsByTagName(&quot;a&quot;);

       for (var i=0; i&lt;Links.length; i++){
              var linkurl = links[i].getAttribute(&quot;href&quot;);
              var currenturl = window.location.href;
              if (currenturl.indexOf(linkurl) != -1) {
                    links[i].className = &quot;here&quot;;
                    var linktext = links[i].lastChild.nodeValue.toLowerCase();
                    document.body.setAttribute(&quot;id&quot;,linktext);
              }
        }
}
</pre>
<p>原理很简单，就是首先获取当前的网址，然后遍历菜单栏，如果当前网址包含了菜单栏内的链接地址，就给这个链接的 class 定义为 here，从而达到高亮的效果。但是我在使用的过程中遇到了一些特殊情况，这段代码就不能满足我的要求了。</p>
<p>先看以下菜单栏：</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;menu&quot;&gt;
   &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;http://yoursite.net/&quot;&gt;首页&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://yoursite.net/2&quot;&gt;第二页&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;http://yoursite.net/3&quot;&gt;第三页&lt;/a&gt;&lt;/li&gt;
   &lt;/ul&gt;
&lt;/div&gt;
</pre>
<p>如果我是用了上述 js 代码，那么当我停留在第二页或者第三页的时候首页同时也会被高亮，这并不是我想要的结果，解决方法有二：</p>
<p>第一种方法，可以在首页的网址后边再加一个 index.php 或者其他的后缀，这样子就可以解决问题，优点是简单，缺点是无法解决更复杂的菜单，比如在一个使用了 URL 静态化的二级页面，这种方法将无法实施。</p>
<p>第二种方法，采用 <a href="http://www.w3school.com.cn/js/jsref_split.asp">split()</a> 获得更精确的定位。</p>
<p><a href="http://www.w3school.com.cn/js/jsref_split.asp">split() </a>方法用于把一个字符串分割成字符串数组，这里我们将网址通过“/”进行拆分，首先是拆分当前网址：</p>
<pre class="brush: jscript; title: ; notranslate">  
var windowLocation = window.location.href.split(&quot;/&quot;);
</pre>
<p>现在我们将网址拆分成一个数组，然后我们将获得网址最后的部分，但是需要注意的是，http://yoursite.net/ 和 http://yoursite.net 是两个不同的网址，如果我们漏掉了其中的一个，可能会导致我们的脚本无效，因此我们需要判断这个网址的末尾是否含有&#8221;/&#8221;。</p>
<p>JS 代码：</p>
<pre class="brush: jscript; title: ; notranslate">
var windowLocation = window.location.href.split(&quot;/&quot;);
var locationLong = windowLocation.length;
var locationHref;

if(windowLocation[locationLong-1] == &quot;&quot;) {
    locationHref = windowLocation[locationLong-2];
} else {
    locationHref = windowLocation[locationLong-1];
}
</pre>
<p>当当前的网址含有&#8221;/&#8221;也就是 windowLocation 的最后一个为&#8221;"的时候，我们就取倒数第二个值，否则我们就取倒数第一个值。取好当前网址的值后，同理我们也要这样子取一下菜单栏的地址：</p>
<pre class="brush: jscript; title: ; notranslate">
var menu = document.getElementById(&quot;menu&quot;);
var menuLinks = menu.getElementsByTagName(&quot;a&quot;);

for(var i=0; i&lt;menuLinks.length; i++){
     var linksHref = menuLinks[i].split(&quot;/&quot;);
     var linksLong = linksHref.length;
     var linksRighthref;

     if(linksHref[linksLong-1] == &quot;&quot;) {
          linksRighthref = linksHref[linksLong-2];
     } else {
          linksRighthref = linksHref[linksLong-1];
     }
     //hight menu
    if(locationHref == linksRighthref) {
          menuLinks[i].className = &quot;here&quot;;
     }
}
</pre>
<p>当然，如果页面还有存在页面翻页参数，如在同一个菜单下默认第一页链接是http://yoursite.net/,第二页链接是http://yoursite.net/?page=2,这样必须在后面的截取段进行进一步的if匹配判断，方法类似。<br />
这样代码就可以解决我们的问题了，优点是实用性较高，但是代码有些复杂。</p>
<p>本文部分内容来自：<a href="http://blog.admin9.com/?p=888">http://blog.admin9.com/?p=888</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.caiyanpei.com/js-nav-highlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
