<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[IE下使用js清空file控件值的几种方法]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Fri, 30 Apr 2010 06:43:21 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	在firefox下使用js清空file控件的value非常简单， 即：obj.value=&quot;&quot;; 就可以了，但在ie下，由于出于安全等方面考虑，file的value被设为了只读，所以js对其不能直接地控制，因此我们只能使用一些变通的方法来解决，网上对此也有好些方法，在此我谈谈自己认为最好的几种。<br/><br/>下面以上传文件格式限制（只对扩展名判断）这一实例来说明。<br/><br/>1、file控件由HTML生成<br/><br/><br/><div class="code">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-<br/><br/>transitional.dtd&quot;&gt;<br/>&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br/>&lt;head&gt;<br/>&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;<br/>&lt;script&gt;<br/>function CheckUpLoadFile(obj) &#123;<br/>DenyExt = &quot;exe&#124;cmd&#124;jsp&#124;php&#124;asp&#124;aspx&#124;html&#124;htm&quot;;<br/>var FileExt = &quot;&quot;;<br/>FileExt = obj.value.substr(obj.value.lastIndexOf(&quot;.&quot;) + 1).toLowerCase();<br/>if (DenyExt.indexOf(FileExt) != -1) &#123;<br/>alert(&quot;You can&#039;t upload the file of the types:&quot; + DenyExt);<br/>if (!window.addEventListener) &#123;&nbsp;&nbsp;&nbsp;&nbsp; <br/>//IE 关键代码在这里！！！<br/>//方法一(此方法最佳！)：<br/>obj.outerHTML+=&#039;&#039;;<br/>//方法二：<br/>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var newObj= obj.cloneNode(true);<br/>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newObj.value=&#039;&#039;; // 设置新控件value为空<br/>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj.parentNode.replaceChild(newObj, obj);<br/><br/>&#125; else &#123;<br/>//非IE<br/>obj.value = &quot;&quot;;<br/>return false;<br/>&#125;<br/><br/>&#125;<br/>&#125;<br/>&lt;/script&gt;<br/>&lt;title&gt;无标题文档&lt;/title&gt;<br/>&lt;/head&gt;<br/>&lt;body&gt;<br/>&lt;span id=&quot;attachments_span&quot;&gt;<br/>&lt;input type=&quot;file&quot; name=&quot;myfile&quot; onchange=&quot;CheckUpLoadFile(this);&quot;&gt;<br/>&lt;/span&gt;<br/>&lt;/body&gt;<br/>&lt;/html&gt;</div><br/><br/>2、file控件由JS动态生成<br/><br/><br/><div class="code">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-<br/><br/>transitional.dtd&quot;&gt;&nbsp;&nbsp;<br/>&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&nbsp;&nbsp;<br/>&lt;head&gt;&nbsp;&nbsp;<br/>&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;&nbsp;&nbsp;<br/>&lt;script&gt;&nbsp;&nbsp;<br/>function addUploadField(name, parentId) &#123;&nbsp;&nbsp;<br/>var f = document.createElement(&quot;input&quot;);&nbsp;&nbsp;<br/>f.type = &quot;file&quot;;&nbsp;&nbsp;<br/>f.name = name;&nbsp;&nbsp;<br/>if (window.addEventListener) &#123; // Mozilla, Netscape, Firefox&nbsp;&nbsp;&nbsp;&nbsp; <br/>f.addEventListener(&quot;change&quot;, function() &#123;&nbsp;&nbsp;<br/>CheckUpLoadFile(f, parentId);&nbsp;&nbsp;<br/>&#125;, false);&nbsp;&nbsp;<br/>&#125; else &#123; // IE&nbsp;&nbsp;&nbsp;&nbsp; <br/>f.attachEvent(&#039;onchange&#039;, function() &#123;&nbsp;&nbsp;<br/>CheckUpLoadFile(f, parentId);&nbsp;&nbsp;<br/>&#125;);&nbsp;&nbsp;<br/>&#125;&nbsp;&nbsp;<br/>f.size = 30;&nbsp;&nbsp;<br/>p = document.getElementById(parentId);&nbsp;&nbsp;<br/>p.appendChild(document.createElement(&quot;br&quot;));&nbsp;&nbsp;<br/>p.appendChild(f);&nbsp;&nbsp;<br/>&#125;&nbsp;&nbsp;<br/><br/>function CheckUpLoadFile(obj) &#123;&nbsp;&nbsp;<br/>DenyExt = &quot;exe&#124;cmd&#124;jsp&#124;php&#124;asp&#124;aspx&#124;html&#124;htm&quot;;&nbsp;&nbsp;<br/>if (obj.value == &quot;&quot;) &#123;<br/>return false;<br/>&#125;<br/>var FileExt = &quot;&quot;;&nbsp;&nbsp;<br/>FileExt = obj.value.substr(obj.value.lastIndexOf(&quot;.&quot;) + 1).toLowerCase();&nbsp;&nbsp;<br/>if (DenyExt.indexOf(FileExt) != -1) &#123;&nbsp;&nbsp;<br/>alert(&quot;You can&#039;t upload the file of the types:&quot; + DenyExt);&nbsp;&nbsp;<br/>if (!window.addEventListener) &#123;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>//IE 关键代码在这里！！！<br/>obj.select();&nbsp;&nbsp;<br/>document.execCommand(&#039;Delete&#039;);&nbsp;&nbsp;<br/>obj.blur();&nbsp;&nbsp;<br/>//obj.outerHTML+=&#039;&#039;; 此方法虽然很安全，但可惜在此不能使用<br/>return false;&nbsp;&nbsp;<br/>&#125; else &#123;&nbsp;&nbsp;<br/>//非IE&nbsp;&nbsp;<br/>obj.value = &quot;&quot;;&nbsp;&nbsp;<br/>return false;&nbsp;&nbsp;<br/>&#125;&nbsp;&nbsp;<br/><br/>&#125;&nbsp;&nbsp;<br/>&#125;&nbsp;&nbsp;<br/>&lt;/script&gt;&nbsp;&nbsp;<br/>&lt;title&gt;无标题文档&lt;/title&gt;&nbsp;&nbsp;<br/>&lt;/head&gt;&nbsp;&nbsp;<br/>&lt;body&gt;&nbsp;&nbsp;<br/>&lt;span id=&quot;attachments_span&quot;&gt;&nbsp;&nbsp;<br/>&lt;input type=&quot;button&quot; name=&quot;add&quot; onclick=&quot;addUploadField(&#039;myFile&#039; ,&#039;attachments_span&#039;);&quot; value=&quot;Add&quot; /&gt;&nbsp;&nbsp;<br/>&lt;/span&gt;&nbsp;&nbsp;<br/>&lt;/body&gt;&nbsp;&nbsp;<br/>&lt;/html&gt; <br/><br/><br/>file.select();<br/>document.execCommand(&#039;Delete&#039;);</div>其他的方法会或多或少的引发一些其他的副作用!<br/><br/>比如:重写innerHTML方法,会引起,再此file域上的事件失效!<br/><br/>调用activex控件会遇到安全性问题!<br/><br/>我写的这个方法最简单,最好用!<br/><br/>转自 http://hi.baidu.com/gudaoxuri/blog/item/0b0095cad9b32a8fc91768c7.html
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] IE下使用js清空file控件值的几种方法]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>