当前位置:首页 > JavaScript > 正文内容

IOS点击事件失效

canca6年前 (2020-04-08)JavaScript683

昨天在做一个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中人们熟悉的类。JS...

《Dom Scripting》读书笔记

  《Dom Scripting》读书笔记 Canca         最近看了一本《Jeremy Keith: DOM Scripting, Web design with JavaScript and the DOM. Apr...

javascript MailTo 邮件技巧

调用email的方法 //<a href="mailto:talantlee@126.com">Email</a>window.location.href="mailto:talantlee@126.com";myform.action="mailto:talant...

javascript IE与FireFox 一些兼容写法

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

javascript 弹出式窗体详解

1>window.prompt(text, value) 簡單的基與模態窗體的對話框,(返回你輸入)   var v=window.prompt("提示","請輸入你的名字")2>window.confirm(text,mess)  模態確認框(返回"是/否...

javascript 中面向对象编程 (类的构造)

javascript 中面向对象编程 (类的构造)

不論是用java,還是c#,又或是vb,構建一個對象都很簡單,都可以采用 classobj =new classobj()的方法構造一個類,然后使用其中的屬性以及方法,其實javascript也是一樣可以實現的。    示例:建立一個js文件,定義一個對象 E...

发表评论

访客

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