<?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>Bolo的博客 &#187; JavaScript</title>
	<atom:link href="http://blog.imbolo.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.imbolo.com</link>
	<description>一个设计爱好者杂七杂八的博客</description>
	<lastBuildDate>Wed, 23 Nov 2011 16:19:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>jQuery选择器详解（一）</title>
		<link>http://blog.imbolo.com/jquery-selectors/</link>
		<comments>http://blog.imbolo.com/jquery-selectors/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 01:57:18 +0000</pubDate>
		<dc:creator>bolo</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.imbolo.com/?p=1890</guid>
		<description><![CDATA[jQuery 的选择器借鉴了大量 CSS1 ~ CSS3 标准的特点，并且整理成一个强大的对象选择工具集。]]></description>
			<content:encoded><![CDATA[<p>jQuery 的选择器借鉴了大量 CSS1 ~ CSS3 标准的特点，并且整理成一个强大的对象选择工具集。但要注意的是如果你的对象名里包含 “<em>#;&#038;,.+*~&#8217;:&#8221;!^$[]()=>|/</em>” 这些元字符时，你必须用 “<em>\\</em>” 进行转义。例如你要选择文档里包含 “<em>name=&#8221;names[]&#8220;</em>” 的 input 元素时，在编写过滤规则时你必须先对 “<em>[]</em>” 进行转义，所以我们应该这样定义选择器：“<em>$(&#8220;input[name=names\\[\\]]&#8221;)</em>”。<span id="more-1890"></span></p>
<h3>泛选择器（*）</h3>
<p>在 jQuery 选择器里，“*” 号是一个通配符，表示所有。例如：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;*&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;border&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;3px solid red&quot;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>上面的一句表示遍历页面里的所有容器，并且全部赋予一个线宽为 3px 的红色实线边框。还有一种情况是我们只需要对某些容器里的内容进行操作，我们可以这样写：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#test&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;*&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;border&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;3px solid red&quot;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>上一句只对 ID 为 test 的容器里的子容器产生效果，为它们加上线宽为 3px 的红色实现边框。</p>
<h3>:animated 选择器</h3>
<p>:animated 选择器是用来选择当前执行 jQuery 动画的元素的，严格来说是一个特征过滤器。例如：在空 HTML 文档里建一些 Div ，其中一个 ID 为 mover ，然后编写以下的 jQuery 语句</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mover&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideToggle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slow&quot;</span><span style="color: #339933;">,</span> animateIt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div:animated&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggleClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;colored&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>上面的代码执行后将先找到 ID 为 mover 的元素，并不断地进行展开和收缩的动画。然后浏览器遍历页面里所有的 Div 标签，如果该 Div （在这个例子里也就是 ID 为 mover 的 Div 了）正在执行动画的话，jQuery 将为这个 Div 加上 class=&#8221;colored&#8221;。具体的 Demo 可以看<a href="http://api.jquery.com/animated-selector/">这个页面</a>。</p>
<h3>属性选择器</h3>
<p>jQuery 的属性选择器是其所有选择器最强大的一个，也是最灵活复杂的，具体情况还可以继续进行细分。</p>
<h4>属性字头选择器（Attribute Contains Prefix Selector）</h4>
<p>jQuery 属性字头选择器的使用格式是 <strong>jQuery(&#8216;[attribute|=value]&#8216;)</strong> ，例如 <strong>jQuery(&#8216;[herflang|=en]&#8216;)</strong> 这句代码执行时将会选择文档中所有含有 herflang 属性，并且 herflang 的值以 “en” 开头的元素，即使 “en” 后面紧跟着连字符 “-” 也能进行选择。</p>
<h4>属性开头选择器（Attribute Starts With Selector）</h4>
<p><strong>jQuery(&#8216;[attribute^=value]&#8216;)</strong> ，用于选择属性的值以某个字符串开头的元素，但和 <strong>jQuery(&#8216;[attribute|=value]&#8216;)</strong> 的区别是，如果 value 的后面是一个连字符的话，这个元素不会被选择。例如 <strong>jQuery(&#8216;[rel^=no]&#8216;)</strong> 将会选择所有 rel 的值以 “no” 开头的元素，但类似于 rel=&#8221;no-****&#8221; 的元素不会被选择。</p>
<h4>属性包含选择器（Attribute Contains Selector）</h4>
<p>基本使用方法为 <strong>jQuery(&#8216;[attribute*=value]&#8216;)</strong>，例如 <strong>jQuery(&#8216;[rel*=no]&#8216;)</strong>，表示所有带有 rel 属性，并且 rel 的值里包含子字符串 “no” 的元素（如 rel=&#8221;nofollow&#8221;，rel=&#8221;yesorno&#8221; 等等）都将会被选择。</p>
<h4>属性单词选择器（Attribute Contains Word Selector）</h4>
<p><strong>jQuery(&#8216;[attribute~=value]&#8216;)</strong>，这个选择器的特别之处在于 value 的值只能必须是一个独立的单词（或者是字符串），例如 <strong>jQuery(&#8216;[rel~=no]&#8216;)</strong> ，此句在执行的时候会选择带有 rel=&#8221;yes or no&#8221; 的元素，但不会选择带有 rel=&#8221;yesorno&#8221; 的元素。这个选择器可以看做属性包含选择器的补充品，用于需要进行严格匹配的时候。</p>
<h4>属性结尾选择器（Attribute Ends With Selector）</h4>
<p><strong>jQuery(&#8216;[attribute$=value]&#8216;)</strong> ，用于选择特定属性的值以某个字符串结尾的元素，例如 <strong>jQuery(&#8216;[rel$=no]&#8216;)</strong> 将会选择 rel 属性的值以 “no” 结尾的元素。</p>
<h4>属性均等选择器（Attribute Equals Selector）</h4>
<p><strong>jQuery(&#8216;[attribute=value]&#8216;)</strong> ，只选择属性的值完全相等的元素，如：<strong>jQuery(&#8216;[rel=nofollow]&#8216;)</strong>，则只选择 rel=&#8221;nofollow&#8221; 的元素，差一点都不行！</p>
<h4>属性非等选择器（Attribute Not Equal Selector）</h4>
<p><strong>jQuery(&#8216;[attribute!=value]&#8216;)</strong> ，和 <strong>:not([attr=value])</strong> 的效果一样，用于选择属性不等于某个值的元素，例如 <strong>jQuery(&#8216;[rel!=nofollow]&#8216;)</strong>，所有 rel=&#8221;nofollow&#8221; 的元素都不会被选择。</p>
<h3>按钮选择器（:button Selector）</h3>
<p><strong>jQuery(&#8216;:button&#8217;)</strong> ，所有的 &#60;input type=&#8221;button&#8221;&#62; 和 &#60;button&#62; 元素都会被选择。</p>
<h3>Checkbox 选择器（:checkbox Selector）</h3>
<p><strong>jQuery(&#8216;:checkbox&#8217;)</strong> ，所有的 &#60;input type=&#8221;checkbox&#8221;&#62; 元素都会被选择。</p>
<h3>Checked 选择器（:checked Selector）</h3>
<p><strong>jQuery(&#8216;:checked&#8217;)</strong> ，可以看做是 Checkbox 选择器的补充，用于选择所有已经被勾选的 Checkbox 对象。</p>
<h3>子元素选择器（Child Selector）</h3>
<p><strong>jQuery(&#8216;parent > child&#8217;)</strong>，只选择 parent 的子元素（下一级元素），不对子元素以下的元素进行选择。</p>
<hr />
<p>近期忙，少更新。</p>
<p  class="related_post_title">Related Posts</p><ul class="related_post"><li><a href="http://blog.imbolo.com/css-turn-off-webkit-browsers-input-highlight-and-textarea-resize/" title="CSS 屏蔽 Webkit 浏览器 input 高亮和 textarea 缩放">CSS 屏蔽 Webkit 浏览器 input 高亮和 textarea 缩放</a></li><li><a href="http://blog.imbolo.com/create-a-simple-ajax-rss-widget-with-jquery-and-yahoo-pipes/" title="利用 Yahoo Pipes 和 jQuery 做一个 RSS 挂件">利用 Yahoo Pipes 和 jQuery 做一个 RSS 挂件</a></li><li><a href="http://blog.imbolo.com/customizing-the-new-wordpress-3-0-custom-menus-adding-different-icons-to-each-link/" title="为 WordPress 3.0 的自定义菜单链接添加栏目图标">为 WordPress 3.0 的自定义菜单链接添加栏目图标</a></li><li><a href="http://blog.imbolo.com/center-multiple-divs-with-css/" title="CSS 多个子框架居中">CSS 多个子框架居中</a></li><li><a href="http://blog.imbolo.com/the-css3-flexible-box-model-2/" title="CSS3 灵活的盒子模型(Flexible Box Module) – 2">CSS3 灵活的盒子模型(Flexible Box Module) – 2</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://blog.imbolo.com/jquery-selectors/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

