以编程方式触发onclick事件?

时间:2009-12-04 19:00:09

标签: javascript javascript-events click onclick

我正在尝试使用iframe模拟异步文件上传。这是我的标记:

<form method="POST" enctype="multipart/form-data" name="change-avatar" action="/dyn/actions/upload?type=profile" target="target-upload">
<input type="file" name="change-avatar-input" style="position: absolute; z-index: 999; cursor: pointer; left: 10px; top: 57px; width: 147px; height: 23px; opacity: 0;"/>
<div style="position: absolute; z-index: 1000; cursor: pointer; left: 10px; top: 57px; width: 147px; height: 23px; opacity: 0;"/>
</form>
<button id="avatar-src" class="browse-button" type="button">Select File to Upload</button>

我已经采用常规按钮并在其上放置一个输入类型=文件,并将不透明度设置为0.此输入提交到隐藏的iframe。问题是文件输入产生意外的光标(预期是指针)并且无法可靠地设置样式。我通过在0不透明度文件输入上放置另一个0不透明度div来解决此问题,并将其设置为生成正确的光标。然后我设置一个事件处理程序来检测0 opacity div上的点击,然后使用click()以编程方式触发文件输入的onclick事件。

问题是在firefox中似乎不支持object.click()(在ie中按预期工作)。

1 个答案:

答案 0 :(得分:0)

查看Valums Ajax上传:http://valums.com/ajax-upload/

该组件已经完成了您尝试做的事情......您应该能够在那里为您的代码获得一些好主意(或者只是使用该组件)。