简体   繁体   English

点击事件在iPad / iPhone / Android上的动态生成元素上不起作用

[英]On click event doesn't work on iPad/iPhone/Android on dynamically built elements

This is how I build the DOM: 这就是我构建DOM的方式:

var projectOverlay = document.createElement("div");
projectOverlay.className = "projectOverlay";
projectOverlay.onclick = function(){openSlider($(this))};   
project.appendChild(projectOverlay);

So... openslider(elem) function works perfectly fine in all browsers except on mobile devices (Android phones, iPhone, iPad etc...) . 所以... openslider(elem)函数在所有浏览器中都可以正常工作,除了移动设备(Android手机,iPhone,iPad等)上。 Is there any other way to bind events to the yet created element or I am doing something wrong here? 还有什么其他方法可以将事件绑定到尚未创建的元素,或者我在这里做错了什么?

Live example - click on contact to get projects and then try to click on one of the projects to expand it. 实时示例 -单击联系人以获取项目,然后尝试单击其中一个项目以将其展开。 Click on About will get you back to the home page. 单击关于将使您返回首页。 That's still messy, but the web site is still in early development stage... 仍然很混乱,但是该网站仍处于早期开发阶段。

Some phones and browsers do not implement the "onclick" event in JavaScript. 某些电话和浏览器未在JavaScript中实现“ onclick”事件。 You should to work with the more touch friendly events, such as "touchstart", "touchmove" and "touchend". 您应该处理更多的触摸友好事件,例如“ touchstart”,“ touchmove”和“ touchend”。

More info here: https://developer.mozilla.org/en-US/docs/DOM/Touch_events 此处提供更多信息: https : //developer.mozilla.org/zh-CN/docs/DOM/Touch_events

Got the same issue in my project. 在我的项目中遇到了同样的问题。 The dynamically created elements were not getting the click events, except on iOS6 (so something might have been fixed there). 除了在iOS6上以外,动态创建的元素没有获取click事件(因此可能已在此处修复了某些问题)。 The work around I have found is to force a transform on the div that contains the dynamically created elements. 我发现的解决方法是对包含动态创建的元素的div强制进行转换。 The div that contains my dynamically created elements did exist when the page loaded. 当页面加载时,包含我动态创建的元素的div确实存在。 Assuming it has an id attribute with the value "container", the code would be as follows 假设它有一个id属性,值为“ container”,代码如下

var container = document.getElementById("container");
container.style.webkitTransitionDuration = "0";
container.style.webkitTransform = "translateY(0px)";

Then the dynamically created elements under the container were getting click events. 然后,在容器下动态创建的元素将获得点击事件。

尝试在appendChild之后移动click事件绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM