<?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>A colourful bubble? &#187; IE8</title>
	<atom:link href="http://www.paopao.name/tag/ie8/feed" rel="self" type="application/rss+xml" />
	<link>http://www.paopao.name</link>
	<description>生活 读书 新知</description>
	<lastBuildDate>Wed, 20 May 2009 02:22:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>WordPress 搜索框添加文字提示</title>
		<link>http://www.paopao.name/programme/javascript/wordpress-searchbox-text-prompt.html</link>
		<comments>http://www.paopao.name/programme/javascript/wordpress-searchbox-text-prompt.html#comments</comments>
		<pubDate>Sun, 10 May 2009 11:29:51 +0000</pubDate>
		<dc:creator>paopao</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WebDevelop]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[IE8]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[searchbox]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.paopao.name/?p=165</guid>
		<description><![CDATA[搜索框里显示文字提示是目前常见的一种技术了，但是实现的方法有很多种，mg12 介绍一个使用 jQuery 添加文字提示的方法。
这个方法相比于原来直接添加 &#8220;input&#8221; 元素的值为提示文字的... ]]></description>
			<content:encoded><![CDATA[<p>搜索框里显示文字提示是目前常见的一种技术了，但是实现的方法有很多种，<a href="http://www.neoease.com/" target="_blank">mg12</a> 介绍一个<a href="http://www.neoease.com/wordpress-searchbox-tip/" target="_blank">使用 jQuery 添加文字提示</a>的方法。</p>
<p>这个方法相比于原来直接添加 &#8220;input&#8221; 元素的值为提示文字的办法，提升了可用性和可访问性，不过也存在两个缺点。一个是 mg12 自己也提到的不方便国际化的问题；另一个则是，如果用户要搜索的关键词和默认的文字提示相同时，则无法进行搜索。当然，你也可以认为第二个问题是我钻牛角尖，也许根本没有人会用默认的文字提示作为搜索关键词。其实我也认为不会出现这种情况，我只是想提出另一种思路来实现搜索框的文字提示，这个办法应该可以解决上面提到的两个问题。</p>
<p>下面是我修改我这个主题的搜索框提示的方法：</p>
<p>首先我需要对搜索框部分的主题文件做一点小小的修改</p>
<pre class="brush: php;">
&lt;form method=&quot;get&quot; id=&quot;searchform&quot; action=&quot;&lt;?php bloginfo('url'); ?&gt;/&quot;&gt;
&lt;div&gt;&lt;input type=&quot;text&quot; value=&quot;&lt;?php the_search_query(); ?&gt;&quot; name=&quot;s&quot; id=&quot;s&quot; title=&quot;&lt;?php _e('Type text to search here...'); ?&gt;&quot; /&gt;
&lt;input type=&quot;submit&quot; id=&quot;searchsubmit&quot; value=&quot;Search&quot; /&gt;
&lt;/div&gt;
&lt;/form&gt;
</pre>
<p>以上是我这个主题文件的 searchform.php 文件的内容，我做的修改只是给 &#8220;input&#8221; 元素添加了一个属性</p>
<blockquote><p> title=&#8221;&lt;?php _e(&#8217;Type text to search here&#8230;&#8217;); ?&gt;&#8221; </p></blockquote>
<p>这样就解决了提示文字的国际化问题，如果在语言文件里有定义就可以直接使用了。</p>
<p>然后就是 jQuery 的部分</p>
<pre class="brush: jscript;">
jQuery(document).ready(function(){
    // DOM 加载完毕时添加搜索提示
    // 修改搜索框的背景色为透明，以便显示其下方的文字
    $(&quot;input#s&quot;).css({backgroundColor:&quot;transparent&quot;});

    $(&quot;input#s&quot;)
        // 将搜索框的上层元素改为相对定位
        .parent().css({position:&quot;relative&quot;})
        // 添加一个绝对定位的 span 至搜索框的下方来显示文字提示
        .append('&lt;span id=&quot;searchtip&quot; style=&quot;position:absolute; top:2px; left:10px; color:#999; display:none; z-index:-1;&quot;&gt;' + $(&quot;input#s&quot;).attr(&quot;title&quot;) + '&lt;/span&gt;');

    // 如果搜索框内容为空则显示提示
    if($(&quot;input#s&quot;).val() == &quot;&quot;) {
        $(&quot;#searchtip&quot;).show();
    }

    // 当鼠标聚焦在搜索框
    $(&quot;input#s&quot;).focus(function(){
        // 如果搜索框内容为空则隐藏提示
        if($(this).val() == &quot;&quot;) {
            $(&quot;#searchtip&quot;).hide();
        }
    }).blur(function(){
        // 如果搜索框内容为空则显示提示
        if($(this).val() == &quot;&quot;) {
            $(&quot;#searchtip&quot;).show();
        }
    });

    // 当搜索表单提交时
    $(&quot;#searchform&quot;).submit(function(){
        // 如果搜索框内容为空则不提交
        if($(&quot;input#s&quot;).val() == &quot;&quot;) {
            return false;
        }
        return true;
    });

    // 解决 IE 8 透明背景色的问题
    $(&quot;#searchtip&quot;).click(function(){$(&quot;input#s&quot;).trigger(&quot;focus&quot;);});
});
</pre>
<p>这个方法的原理也很简单，就是在搜索框的后面加入一个绝对定位的 &#8220;span&#8221; 元素来显示文字提示，文字提示的内容则是通过获取搜索框的 title 值来添加。</p>
<p>以上就完成了搜索框文字提示的添加，下面是我在这个过程中遇到的一个问题。</p>
<p>注意到上面的最后一行代码了吗，是为了解决在 IE 8 下的一个问题的，本来可以不需要这行代码的，就是为了万恶的 IE &#8230;</p>
<p>问题是这样的，当一个绝对定位的元素位于一个 &#8220;input&#8221; 元素的下方时，在 IE 8 下你依旧可以用鼠标访问到这个元素，导致点击搜索框时无法获得焦点，在其他<a href="http://www.paopao.name/browsers">我测试的浏览器</a>中都没有这个问题。所以在 IE 8 下，需要为提示文字的 &#8220;span&#8221; 元素额外的绑定一个监听事件。而且有点奇怪的是，把 &#8220;input&#8221; 元素换成 &#8220;div&#8221; 元素后，IE 8 下的表现又和其他浏览器一致了。</p>
<p>关于这个问题我做了一个简单的<a href="http://paosha.net/demo/single/searchboxtip.html" target="_blank">演示 Demo</a> 页面，有兴趣的可以去看看，我手头没有 IE 7 和 IE 6 ，不知道这两个浏览器是不是存在同样的问题，另外如果你有解决这个问题的办法，也请多多指教。</p>
<img src="http://www.paopao.name/?ak_action=api_record_view&id=165&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.paopao.name/programme/javascript/wordpress-searchbox-text-prompt.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
