<?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; javascript</title>
	<atom:link href="http://www.caiyanpei.com/tag/javascript/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>
		<item>
		<title>jquery实现有默认内容的展开收起效果</title>
		<link>http://www.caiyanpei.com/jquery-flip/</link>
		<comments>http://www.caiyanpei.com/jquery-flip/#comments</comments>
		<pubDate>Sat, 11 Sep 2010 09:34:55 +0000</pubDate>
		<dc:creator>tsai</dc:creator>
				<category><![CDATA[技术]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[设计]]></category>

		<guid isPermaLink="false">http://cyp.me/?p=225</guid>
		<description><![CDATA[前些天在实现一个网站的需求：1、展示一个分类里边的条目；2、默认显示前十条，并显 &#8230; <a href="http://www.caiyanpei.com/jquery-flip/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>前些天在实现一个网站的需求：1、展示一个分类里边的条目；2、默认显示前十条，并显示一个展开按钮，点击展开按钮，点击后下拉显示该分类下所有条目；3、展开后，按钮变为收起按钮，点击收起恢复为默认显示条目。</p>
<p>试图想用jquery的<a href="http://www.w3school.com.cn/jquery/effect_slidetoggle.asp">slideToggle()</a>、<a href="http://www.w3school.com.cn/jquery/effect_animate.asp">animate()</a>函数实现，发现行不通，处理不了剩余内容的展开收起。后来想到通过联系展开按钮的class与主题内容的id匹配，在按钮的click事件里通过修改css和jquery的<a href="http://www.w3school.com.cn/jquery/effect_animate.asp">animate()</a>函数结合实现想要的效果，代码如下(这边显示一段文字，默认显示文字前面的一部分内容)：</p>
<p>jquery部分：</p>
<pre class="brush: jscript; title: ; notranslate">
$(&quot;.more-btn&quot;).click(function(){
			 if($(&quot;#&quot;+this.parentNode.className).css(&quot;height&quot;)==&quot;100px&quot;){
			  	$(&quot;#&quot;+this.parentNode.className).css(&quot;height&quot;,&quot;auto&quot;);
			  	this.value=&quot;收起&quot;;
			  }
			 else{
			 	$(&quot;#&quot;+this.parentNode.className).animate({height:&quot;100px&quot;},&quot;slow&quot;);
			 	this.value=&quot;展开&quot;;
			 	}
		  });
</pre>
<p>css部分：</p>
<pre class="brush: css; title: ; notranslate">
body {
			margin:0;
			padding:40px 40px;
			background:#fff;
			font:Arial, Helvetica, sans-serif;
			font-size:13px;
			line-height:180%;
		}
		.case{
			width:240px;
		}
		.entry{
			clear: both;
			line-height: 1.6em;
			width:223px;
			height:100px;
			overflow:hidden;
			line-height:18px;
			border-top:#ff9933 1px solid;
			border-left:#ff9933 1px solid;
			border-right:#ff9933 1px solid;
			padding-left:5px;
		}
		.more-btn{
			padding:0;
			width:230px;
			border-left:#ff9933 1px solid;
			border-right:#ff9933 1px solid;
			height:27px;
			background:#f4dc92;
			font-size:14px;
			font-weight:bolder;
			color:#000;
		}             
</pre>
<p>html代码：</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;demo&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div class=&quot;case&quot;&gt;    	
	&lt;div class=&quot;entry&quot; id=&quot;entry-1&quot;&gt;	
	      	显示的内容在此。。。
	&lt;/div&gt;&lt;!--entry--&gt;
	&lt;div class=&quot;entry-1&quot;&gt;&lt;input type=&quot;button&quot;  class=&quot;more-btn&quot; value=&quot;更多&quot; /&gt;&lt;/div&gt;	
	&lt;/div&gt; &lt;!--case--&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>demo：<a href="http://www.xiuchezu.com/wp-content/uploads/2010/09/demo.html">猛击此查看demo</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.caiyanpei.com/jquery-flip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
