Javascript 如何捕捉多个图像上的点击?
我有一个iOS uiwebview,它有多个图像映射,我需要捕捉点击,因此我可以在不同的iOS设备上处理缩放。我安装的单击处理程序在第一个映像上工作,但在后续映像上不工作。如何使单击处理程序在多个图像上工作?相关代码如下:Javascript 如何捕捉多个图像上的点击?,javascript,jquery,uiwebview,imagemap,Javascript,Jquery,Uiwebview,Imagemap,我有一个iOS uiwebview,它有多个图像映射,我需要捕捉点击,因此我可以在不同的iOS设备上处理缩放。我安装的单击处理程序在第一个映像上工作,但在后续映像上不工作。如何使单击处理程序在多个图像上工作?相关代码如下: $.fn.imageMapSetup2 = function () { $('img').each(function () { if (typeof ($(this).attr('usemap')) == 'undefined') {
$.fn.imageMapSetup2 = function () {
$('img').each(function () {
if (typeof ($(this).attr('usemap')) == 'undefined') {
return;
}
var img = $(this);
// add click handler
img.on('click', function (event) {
img.imgClick(event);
});
});
};
$.fn.imgClick = function (mouseDown) {
mouseDown.preventDefault();
var $img = this;
var map = $img.attr('usemap').replace('#', '');
$('map[name="' + map + '"]').find('area').each(function () {
var $this = $(this),
coords = $this.attr('coords').split(',');
// lots of scaling code omitted
if (mouseX >= left && mouseX <= right &&
mouseY >= top && mouseY <= bottom) {
window.location = $this.attr('href');
}
});
};
$.fn.imageMapSetup2=函数(){
$('img')。每个(函数(){
if(typeof($(this).attr('usemap'))=='undefined'){
返回;
}
var img=$(本);
//添加单击处理程序
“点击”功能(事件)上的图像{
img.imgClick(事件);
});
});
};
$.fn.imgClick=函数(鼠标向下){
mouseDown.preventDefault();
var$img=此;
var map=$img.attr('usemap').replace('#','');
$('map[name=“”+map+“]”)。查找('area')。每个(函数(){
变量$this=$(this),
coords=$this.attr('coords').split(',');
//省略了许多缩放代码
如果(mouseX>=left&&mouseX=top&&mouseY将单击事件侦听器添加到图像的父元素。这可能是body元素。将事件作为参数传递。然后,检查事件,并使用该变量更改图像
document.addEventListener("click", function (event) {
if (!event.target.tagName === "img") return;
if (typeof event.target.getAttribute("usemap") == "undefined") {
return;
}
imgClick(event);
});
请也张贴您的HTML样本,以便我们可以更好地了解正在发生的事情