清空file input框

测试环境:
OS --> winXP
Browsers --> IE6+, FF 3.0.11, FF 3.5, Opera 9.64, Opera 10 beta 2, Safari 4, Chrome 2.0.172

xhtml:
<input type="file" value="选择文件" id="fileInput" />
<button type="button" id="btnClear">清空文件域</button>

JS:
<script type="text/javascript">
var btn = document.getElementById("btnClear");
var file = document.getElementById("fileInput");
btn.onclick = function() {
  // for IE, Opera, Safari, Chrome
  if (file.outerHTML) {
    file.outerHTML = file.outerHTML;
  } else { // FF(包括3.5)
    file.value = "";
  }
}
</script>

或者

var file = $("#file")
file.after(file.clone().val(""));
file.remove();

参考: http://blog.163.com/thinker_creator/blog/static/101933149200961585128272/
    http://www.jb51.net/article/35491.htm

上一篇:chrome扩展程序开发


下一篇:tomcat启动项目内存溢出问题