按钮在iPhone上无法正常工作(可能还有其他触摸屏)
Button not working properly on iPhone (and possibly other touch screens)
我有一个Map按钮,您可以单击(或点击)它来显示带有嵌入式Google Map的模态。在不同的浏览器中,它在桌面上的工作原理是一样的,但在iPhone(可能还有其他触摸屏)上,我必须点击两次按钮。
编辑:尝试使用"ontouchend"作为触发器,使用"touchend"关闭模态。现在模态一打开就关闭。
HTML:
<div class="map-modal" id="map-modal">
<div class="map-container">
<div class="the-map" id="map-canvas"></div>
<div class="hide-btn hide-btn--map" onclick="closeModal();"><span class="close hairline"></span><div class="hidden-content">Hide the map</div></div>
</div>
</div>
按钮:
<div class="button button--map" onmousedown="calcRoute('<?php echo $location[address1]; ?>', '<?php echo $location[address2]; ?>');">Map</div>
JavaScript:
function calcRoute(start, end) {
var e = document.getElementById("map-modal");
e.style.display = 'block';
$("body").addClass("modal-open");
google.maps.event.trigger(map, 'resize');
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
alert("Sorry, no walking route can be found between these locations");
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
听起来像touchstart和mousedown都在开火。此线程具有一些选项,用于在移动设置中抑制mousedown事件。onclick和ontouchstart同时启动
解决方案(不完美)
解决方案已更新,请参阅下文。
解决方案是防止事件在DOM树中冒泡(我以前从未听说过事件冒泡)。在谷歌上搜索并发现这一点,为我指明了正确的方向。这很简单。只需在事件触发器上使用this.stopPropagation();
。
我在这篇文章中保留了以前的解决方案,以展示我所尝试的,并可能让男性更容易理解事件传播(或"事件冒泡")在这种情况下是如何工作的。
如果有人有更优雅的解决方案,请随时分享。
触发器事件按钮
<div class="button button--map right"
ontouchstart="this.stopPropagation(); calcRoute('Citybox+Oslo,+Prinsens+gate+6,+0152+Oslo,+Norway', '<?php echo $location[address]; ?>');"
onclick="calcRoute('Citybox+Oslo,+Prinsens+gate+6,+0152+Oslo,+Norway', '<?php echo $location[address]; ?>');"
>Map</div>
相关文章:
- 如何添加浮动和非浮动,其他
- 与其他库的jQuery.noConflict()
- 播放当前视频时如何停止其他视频?JavaScript
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- 除修剪外的其他功能
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- Array.length似乎不起作用;console.log则显示其他情况
- 如何消除代码中的未定义和其他问题
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- Jquery未定义函数正在停止其他操作
- 如何将我的javascript库公开给其他客户端使用
- Javascript阻止其他Javascript代码
- Jquery在触摸屏上添加类和数据
- chart.series[id].remove()无法刷新高位图表/高位股票中其他系列的图例属性
- 如何“;过滤器”;或者以其他方式重构该数据
- java脚本在Chrome和其他浏览器中对LocaleTimeString的不同行为
- 按钮在iPhone上无法正常工作(可能还有其他触摸屏)