<?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>南芝 - 我才12岁（飞鸟尽，良弓藏，2011，背包去旅行！） &#187; split()</title>
	<atom:link href="http://www.12sui.cn/tag/split/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.12sui.cn</link>
	<description>南芝的前端技术学习经验</description>
	<lastBuildDate>Sun, 08 Jan 2012 09:22:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>高亮当前位置</title>
		<link>http://www.12sui.cn/develop/%e9%ab%98%e4%ba%ae%e5%bd%93%e5%89%8d%e4%bd%8d%e7%bd%ae/</link>
		<comments>http://www.12sui.cn/develop/%e9%ab%98%e4%ba%ae%e5%bd%93%e5%89%8d%e4%bd%8d%e7%bd%ae/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 09:51:02 +0000</pubDate>
		<dc:creator>南芝</dc:creator>
				<category><![CDATA[编码]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[split()]]></category>
		<category><![CDATA[高亮]]></category>

		<guid isPermaLink="false">http://www.12sui.cn/?p=120</guid>
		<description><![CDATA[Jeremy Keith 在《JavaScript DOM编程艺术》一书中为我们做了一个小的例子，例子中使用了一段简单的高亮当前位置的 js 代码，代码如下： function highlightPage() { if (!document.getElementsByTagName) return false; if (!document.getElementById) return false; if (!document.getElementById("navigation")) return false; var nav = document.getElementById("navigation"); var links = nav.getElementsByTagName("a"); for (var i=0; i&#60;links.length; i++) { var linkurl = links[i].getAttribute("href"); var currenturl = window.location.href; if (currenturl.indexOf(linkurl) != -1) { links[i].className = "here"; var linktext = links[i].lastChild.nodeValue.toLowerCase(); [...]]]></description>
			<content:encoded><![CDATA[<p>Jeremy Keith 在《JavaScript DOM编程艺术》一书中为我们做了一个小的例子，例子中使用了一段简单的高亮当前位置的 js 代码，代码如下：</p>

<p><pre>function highlightPage() {
    if (!document.getElementsByTagName) return false;
    if (!document.getElementById) return false;
    if (!document.getElementById("navigation")) return false;
    var nav = document.getElementById("navigation");
    var links = nav.getElementsByTagName("a");
    for (var i=0; i&lt;links.length; i++) {
    var linkurl = links[i].getAttribute("href");
    var currenturl = window.location.href;
    if (currenturl.indexOf(linkurl) != -1) {
        links[i].className = "here";
        var linktext = links[i].lastChild.nodeValue.toLowerCase();
        document.body.setAttribute("id",linktext);
    }
    }
}</pre></p>

<p>原理很简单，就是首先获取当前的网址，然后遍历菜单栏，如果当前网址包含了菜单栏内的链接地址，就给这个链接的 class 定义为 here，从而达到高亮的效果。但是我在使用的过程中遇到了一些特殊情况，这段代码就不能满足我的要求了。</p>

<p>先看一下我的菜单栏：</p>

<p><pre>&lt;div id="menu"&gt;
    &lt;ul&gt;&lt;li&gt;&lt;a href="http://www.12sui.cn/"&gt;首页&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.12sui.cn/2"&gt;第二页&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.12sui.cn/3"&gt;第三页&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;/div&gt;</pre></p>

<p>如果我是用了上述 js 代码，那么当我停留在第二页或者第三页的时候首页同时也会被高亮，这并不是我想要的结果，解决方法有二：</p>

<p>第一种方法，我可以在首页的网址后边再加一个 index.php 或者其他的后缀（我的博客现在就这么搞。。。），这样子就可以解决问题，优点是简单，缺点是无法解决更复杂的菜单，比如我在一个使用了 URL 静态化的二级页面，这种方法将无法实施。</p>

<p>第二种方法，采用 split() 获得更精确的定位。</p>

<p><span id="more-120"></span></p>

<p>split() 方法用于把一个字符串分割成字符串数组，这里我们将网址通过“/”进行拆分，首先是拆分当前网址：</p>

<p><pre>var windowLocation = window.location.href.split("/");</pre></p>

<p>现在我们将网址拆分成一个数组，然后我们将获得网址最后的部分，但是需要注意的是，http://www.12sui.cn/ 和 http://www.12sui.cn 是两个不同的网址，如果我们漏掉了其中的一个，可能会导致我们的脚本无效，因此我们需要判断这个网址的末尾是否含有“/”。</p>

<p>JS 代码：</p>

<p><pre>var windowLocation = window.location.href.split("/");
var locationLong = windowLocation.length;
var locationHref;
if(windowLocation[locationLong-1] == "") {
    locationHref = windowLocation[locationLong-2];
} else {
    locationHref = windowLocation[locationLong-1];
}</pre></p>

<p>当当前的网址含有“/”也就是 windowLocation 的最后一个为“”的时候，我们就取倒数第二个值，否则我们就取倒数第一个值。取好当前网址的值后，同理我们也要这样子取一下菜单栏的地址：</p>

<p><pre>var menu = document.getElementById("menu");
var menuLinks = menu.getElementsByTagName("a");
for(var i=0; i&lt;menuLinks.length; i++) {
    var linksHref = menuLinks[i].href.split("/");
    var linksLong = linksHref.length;
    var linksRighthref;
    if(linksHref[linksLong-1] == "") {
        linksRighthref = linksHref[linksLong-2];
    } else {
        linksRighthref = linksHref[linksLong-1];
    }
    //hight menu
    if(locationHref == linksRighthref) {
        menuLinks[i].className = "here";
    }
}</pre></p>

<p>这段代码就可以解决我们的问题了，优点是实用性较高，但是代码有些复杂。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.12sui.cn/develop/%e9%ab%98%e4%ba%ae%e5%bd%93%e5%89%8d%e4%bd%8d%e7%bd%ae/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

