模拟点击事件动态增删元素
2017-02-05 17:12
260 查看
<tr> <td align="right">商品图像</td> <td> <a href="javascript:void(0)" id="selectFileBtn">添加附件</a> <div id="attachList" class="clear"></div> </td> </tr>
<script type="text/javascript"> $("#selectFileBtn").click(function(event) { $fileFiled=$('<input type="file" name="thumbs[]"/>' ); $fileFiled.hide(); $("#attachList").append($fileFiled); $fileFiled.change(function(event) { //console.log("change"); $path=$(this).val(); $filename=$path.substring($path.lastIndexOf("\\")+1); if($path!=""){ $attachItem=$('<div class="attachItem"><div class="left">a.gif</div><a href="javascript:void(0)" title="删除附件">删除</a></div>'); $attachItem.find(".left").html($filename); $("#attachList").append($attachItem); $a=$("#attachList>.attachItem").find("a"); //console.log("第二次算a数量为:"+$a.length); $a.click(function(){ //console.log("触发了click事件"); $(this).parents('.attachItem').prev('input').remove(); $(this).parents('.attachItem').remove(); }) } }); //注意,trigger()写在change后面,否则有些浏览器会出现问题 $fileFiled.trigger('click'); //$fileFiled.trigger('change'); //egde 浏览器不写这句不能显示?! $a=$("#attachList>.attachItem").find("a"); if($a.length==0){ //console.log("a数量为零,再次手动触发change事件"); $fileFiled.trigger('change'); } //event.stopPropagation(); }); </script>
相关文章推荐
- 不定宽元素居中的方法
- 如何使用VS将项目生成一个安装包?
- Tango 开发指南 — 深度感知原理
- python之pandas学习
- 读取服务器中的图片,旋转后保存服务器,替换原图片
- spring maven 搭建dubbo框架 消费端
- Python学习札记(十六) 高级特性2 迭代
- Professional C# 6 and .NET Core 1.0 - Creating Hello, World! with Visual Studio
- hive Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException
- 1047. 编程团体赛(20)
- MTK ISP 分析
- 栈之顺序栈基本操作
- [置顶] React Native开源项目 「漫画书」
- requirejs 配制
- 小型计算器A small software project
- Android Shimmer学习
- C#中使用@替代使用转意字符
- Windows核心编程笔记(十五)应用程序中使用虚拟内存
- 实现请求同一视频时识别播放或下载行为
- 是指针与地址的关系问题运算