当前位置:首页 > JavaScript

IOS点击事件失效

canca7年前 (2020-04-08)JavaScript958

昨天在做一个H5页面调查问卷的时候碰到了一个很奇葩的问题,给一个元素添加点击事件的时候在安卓上面可以实现添加元素,但是在IOS上面没有起作用。一开始我是使用的下面的第一种方法写的点击事件,但是并不可以在新添加的元素上面实现点击事件。所以我该用了绑定方法on来给添加元素按钮添加点击事件。是可以实现的。但是到了IOS上面失去了点击事件,又去度娘查了下才知道原因,因为IOS上面没有click点击事件,在IOS上面使用touch事件来实现点击事件的。所以这就简单多了。具体解决方法如下

1.$(".box").click(function(){
    var index = $(this).parent('.qa_item_add_boxs').parent('.qa_item_aed_boxs').children().length;
    $(this).parent('.qa_item_add_boxs').parent('.qa_item_aed_boxs').append(`
        <div class="qa_item_add_boxs">
	          <div class="qa_item_add_nums">${index + 1}.</div>
		     <input class="qa_item_add_input" type="text" placeholder="商品名" />
		     <input class="qa_item_add_input" type="text" placeholder="对应企业名" />
		     <div class="qa_add_imgBox">
			     <img class="qa_item_add_img" id="eight_add" src="res/img/add.png"/>
		     </div>
	    </div>
    `);
 })
 
 
 
 
2.$(document).on('click','.qa_add_imgBox',function(){          
    var index = $(this).parent('.qa_item_add_boxs').parent('.qa_item_aed_boxs').children().length;
    $(this).parent('.qa_item_add_boxs').parent('.qa_item_aed_boxs').append(`
	    <div class="qa_item_add_boxs">
		    <div class="qa_item_add_nums">${index + 1}.</div>
		    <input class="qa_item_add_input" type="text" placeholder="商品名" />
		    <input class="qa_item_add_input" type="text" placeholder="对应企业名" />
		    <div class="qa_add_imgBox">
			    <img class="qa_item_add_img" id="eight_add" src="res/img/add.png"/>
		    </div>
	    </div>
    `);
})

解决IOS上面不能实现点击事件的方法

1.把点击元素改为button或者a链接 (这种方法暂未发现其它问题,但并不推荐使用)

2.添加touchstart事件document.body.addEventListener('touchstart', function() {}) (暂不推荐)

3.给点击元素添加属性cursor:pointer即可 (推荐使用,简单方便,无副作用)


扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/712.html

标签: JavaScript
分享给朋友:

“IOS点击事件失效” 的相关文章

JavaScript 函数原型对象

    以下是一篇本人在Qzone里写下的文章,现在放到这里来,欠丑了。希望对一些初学者有一点点帮助。     今天给大家说说JavaScript中的类。类?没错。JavaScript中的函数原型对象就是OOP中人们熟悉的类。..…

javascript IE与FireFox 一些兼容写法

1>获取控件用document.getElementById,不用document.all(FF等浏览器不支持)2><button> 会被firefox解释为提交form或者刷新页面,需要写标准<button type="button">3>使用childN…

javascript在IE和Firefox中的兼容考虑

1.document.formName.item("itemName") 问题说明:IE下,可以使用document.formName.item("itemName")或document.formName.elements["elementName"];Firefox下,只能使用docum…

event事件兼容mozilla firefox的问题

本来一个写好了的脚本在IE下正常,可是在mozilla firefox中我们发现程序报类似如下的错误: event is not defined obj has no properties 原因是Firefox中使用了不同的事件对象模型,不同于IE Dom,用的是W3C Dom. 下是我在网上找到的…

JavaScript 为网页添加快捷键

为网页添加快捷键即为网页添加onkeydown事件。 FireFox里获取键入键的ASCII码与IE获取是有些差别的。因为FF里没有Event对象。 但FF里事件触发方法中默认传送其事件对象。 即我们在JS里写该事件方法时,要多加一个参数。 例: <sc...…

JavaScript对粘贴板的全操作

<html>    <head>        <script language="javascript">function copy_clip(copyObjectName…

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。