简体   繁体   English

$(“。element”)。first()。trigger('click')-无法使用iPad

[英]$(“.element”).first().trigger('click') - not working iPad

I am using: 我在用:

$(".first-image-gallery").first().trigger('click');

To create the action of clicking on a gallery of images. 创建单击图库的操作。

The reason for this is I am using a 'Start Gallery' button elsewhere on the page and want this button to open up the first image of the gallery and then the user can move forward and back as they please. 原因是我在页面其他位置使用“开始画廊”按钮,希望该按钮打开画廊的第一张图片,然后用户可以根据需要前后移动。

I am using iLightBox for the gallery and can't find anything that it has build in to open up the gallery from another button or action via jQuery. 我正在为画廊使用iLightBox,无法找到它内置的任何内容通过jQuery通过另一个按钮或操作来打开画廊。

The current code I am using works fine elsewhere on desktop, but mobile iPad it wont work. 我正在使用的当前代码在台式机上的其他地方都可以正常工作,但在移动iPad上无法正常工作。

This is Mobile Safari oddity. 这是Mobile Safari怪胎。 Click events don't work properly on any elements without cursor: pointer; 单击事件在没有cursor: pointer;任何元素上无法正常工作cursor: pointer; CSS property set on them. 在它们上设置CSS属性。

Read more: http://www.shdon.com/blog/2013/06/07/why-your-click-events-don-t-work-on-mobile-safari 了解更多: http : //www.shdon.com/blog/2013/06/07/why-your-click-events-don-t-work-on-mobile-safari

This works on every standards-compliant browser. 这适用于所有符合标准的浏览器。 Mobile Safari chooses to differ, however. 但是,移动Safari选择不同。 It does not generate click events for elements that do not have either or both of: 它不会为没有以下一项或两项的元素生成点击事件:

-Directly bound onclick handler. -直接绑定onclick处理程序。

-The cursor property set to pointer in CSS. -cursor属性设置为CSS中的指针。

Therefore, your solution seems to be: 因此,您的解决方案似乎是:

.first-image-gallery {
  cursor: pointer;
}

I (almost) always resort to including cssua javascript library and including this bit of css, which solves this problem globally (page-wide), applying cursor: pointer; 我(几乎)总是求助于包括cssua javascript库并包含这部分css,从而通过使用cursor: pointer;来全局(整个页面)解决此问题cursor: pointer; on whole page in Mobile Safari (but not in other browsers): 在Mobile Safari的整个页面上(但在其他浏览器中则没有):

html.ua-safari.ua-ios {
  cursor: pointer;
}

Try touchstart instead: 尝试使用touchstart:

$(".first-image-gallery").first().trigger('touchstart');

And use this in click function too like .on('click touchstart',callback); 并在.on('click touchstart',callback);也使用此功能.on('click touchstart',callback);

I have the same issue and I have tried set cursor:pointer to the element, it doesn't work. 我有同样的问题,我尝试将cursor:pointer设置为元素,它不起作用。

It is pretty obvious that trigger 'touchstart' works pretty well with jQuery, but the problem is that I need to 'click' an existing element (button) programmatically, so I need to trigger 'click'. 很明显,触发“ touchstart”与jQuery配合得很好,但问题是我需要以编程方式“单击”现有元素(按钮),因此需要触发“单击”。

In the end I found that using native javascript code works perfectly, so you can try: 最后,我发现使用本机javascript代码非常有效,因此您可以尝试:

document.querySelectorAll('.first-image-gallery')[0].click();

At least it works immediately for my case. 至少对于我的情况,它可以立即生效。

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

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