<?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>wlsy &#187; 技巧</title>
	<atom:link href="http://wlsy.me/tag/%e6%8a%80%e5%b7%a7/feed/" rel="self" type="application/rss+xml" />
	<link>http://wlsy.me</link>
	<description>还活着呢</description>
	<lastBuildDate>Mon, 05 Jul 2010 16:24:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>利用javasript凸显当前页</title>
		<link>http://wlsy.me/1605/</link>
		<comments>http://wlsy.me/1605/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 06:31:51 +0000</pubDate>
		<dc:creator>wlsy</dc:creator>
				<category><![CDATA[Web前端]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[技巧]]></category>

		<guid isPermaLink="false">http://www.g9net.com/?p=1605</guid>
		<description><![CDATA[这是一个简单而且实用的小技巧，至少我只这么觉得的:)。最早好像是是在DOM Scripting 里面看到的。好吧，废话不说了，直接上代码好好享用。
比如导航栏的XHTML代码是这样的：


&#60;ul id="nav"&#62;
   &#60;li&#62;&#60;a href="#"&#62;home&#60;/a&#62;&#60;/li&#62;
   &#60;li&#62;&#60;a href="#"&#62;about&#60;/a&#62;&#60;/li&#62;
   &#60;li&#62;&#60;a href="#"&#62;contact&#60;/a&#62;&#60;/li&#62;
&#60;/ul&#62;


那么如何才能凸显当前页面的呢？给当前页面的链接加个class，然后通过CSS来控制其显示样式，这样行为和表现分开了，以后更改也方便。
送上javascript代码：


function currentPage(){
    if (!document.getElementById("nav")) return false;/*进行必要的测试，避免没有id为nav时候出错*/
    var nav=document.getElementById("nav");
    var links=nav.getElementsByTagName("a");
    var currenturl=window.location.href;/*获取当前页面的地址*/
    for(i=1;i&#60;links.length;i++){
        var url=links[i].getAttribute("href");/*获取链接的href值*/
    [...]]]></description>
			<content:encoded><![CDATA[<p>这是一个简单而且实用的小技巧，至少我只这么觉得的:)。最早好像是是在DOM Scripting 里面看到的。好吧，废话不说了，直接上代码好好享用。</p>
<p>比如导航栏的XHTML代码是这样的：</p>
<div style="margin: 20px 0px 10px; padding: 4px; overflow: auto; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; cursor: text; max-height: 200px; width: 97.5%;">
<div style="border-style: none; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;"><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">ul</span> <span style="color: #ff0000;">id</span><span style="color: #0000ff;">="nav"</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">   <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">a</span> <span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">&gt;</span>home<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;">   <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">a</span> <span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">&gt;</span>about<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">   <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">a</span> <span style="color: #ff0000;">href</span><span style="color: #0000ff;">="#"</span><span style="color: #0000ff;">&gt;</span>contact<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">a</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">li</span><span style="color: #0000ff;">&gt;</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;"><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">&gt;</span></pre>
</div>
</div>
<p>那么如何才能凸显当前页面的呢？给当前页面的链接加个class，然后通过CSS来控制其显示样式，这样行为和表现分开了，以后更改也方便。<span id="more-1605"></span></p>
<p>送上javascript代码：</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;"><span style="color: #0000ff;">function</span> currentPage(){</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">    <span style="color: #0000ff;">if</span> (!document.getElementById(<span style="color: #006080;">"nav"</span>)) <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span>;<span style="color: #008000;">/*进行必要的测试，避免没有id为nav时候出错*/</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;">    <span style="color: #0000ff;">var</span> nav=document.getElementById(<span style="color: #006080;">"nav"</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">    <span style="color: #0000ff;">var</span> links=nav.getElementsByTagName(<span style="color: #006080;">"a"</span>);</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;">    <span style="color: #0000ff;">var</span> currenturl=window.location.href;<span style="color: #008000;">/*获取当前页面的地址*/</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">    <span style="color: #0000ff;">for</span>(i=1;i&lt;links.length;i++){</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;">        <span style="color: #0000ff;">var</span> url=links[i].getAttribute(<span style="color: #006080;">"href"</span>);<span style="color: #008000;">/*获取链接的href值*/</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">        <span style="color: #0000ff;">if</span>(currenturl.indexOf(url)!=-1){<span style="color: #008000;">/*如果链接的href值在当前页面地址中有匹配*/</span></pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;">           links[i].className=<span style="color: #006080;">"current"</span>;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">        }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;">    }</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">}</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;">window.onload=currentPage;<span style="color: #008000;">/*载入页面时加载*/</span></pre>
</div>
</div>
<p>好了，接下来你就可在css里面定义current的样式了，以后要修改也只要在css里面修改</p>
<p>友情提示：如果各位想要在wordpress里面凸显当前页，wordpress有个自带的函数如下：</p>
<div>
<div style="border-style: none; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;">&lt;ul&gt;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">&lt;li &lt;?php <span style="color: #0000ff;">if</span> (is_home()){echo <span style="color: #006080;">'class="current_page_item"'</span>;}?&gt;&gt;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;">&lt;a href=<span style="color: #006080;">"&lt;?php bloginfo('url'); ?&gt;/"</span>&gt;首页&lt;/a&gt;&lt;/li&gt;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; background-color: #f4f4f4; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%;">&lt;?php wp_list_pages(<span style="color: #006080;">'&amp;title_li='</span>); ?&gt;</pre>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-family: Consolas,'Courier New',Courier,Monospace; font-size: 8pt; line-height: 12pt; color: black; width: 100%; background-color: white;">&lt;/ul&gt;</pre>
</div>
</div>
<p>你可以在css定义.current_page_item的样式就可以了</p>
]]></content:encoded>
			<wfw:commentRss>http://wlsy.me/1605/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
