简体   繁体   English

如何让JavaScript生成的图像映射与Internet Explorer一起使用?

[英]How do I get JavaScript-generated image maps to work with Internet Explorer?

I'm using javascript to generate a high resolution grid for an image that I generated on a web server. 我正在使用javascript为我在Web服务器上生成的图像生成高分辨率网格。 The high-resolution grid is composed of a 'map' element with hundreds of 'area' child elements. 高分辨率网格由具有数百个“区域”子元素的“map”元素组成。 Each 'area' element has onmouseover attribute that causes the display of a popup box. 每个'area'元素都有onmouseover属性,导致弹出框的显示。

After assigning the map to the img (via the usemap attribute), Internet explorer ignores the 'onmouseover' attribute of the area elements that I added via javascript. 在将地图分配给img(通过usemap属性)后,Internet Explorer忽略了我通过javascript添加的区域元素的'onmouseover'属性。

The behavior is not caused by syntactical variations between IE and other browsers. 该行为不是由IE和其他浏览器之间的语法变化引起的。 A static map behaves correctly. 静态映射行为正确。 Only the elements that I add dynamically to an existing image map fail to fire their corresponding mouse-over events. 只有我动态添加到现有图像映射的元素才能触发相应的鼠标悬停事件。

How can I get IE to fire the mouse-over event for the added 'area' elements? 如何让IE为添加的“区域”元素触发鼠标悬停事件?

function generate_image_map ( img ) { 
  var tile_width = 8;
  var tile_height = 10;
  var plotarea_left = 40;
  var plotarea_top = 45;
  var column_count = 100;
  var row_count = 120;

  var img_id = YAHOO.util.Dom.getAttribute(img, "id");
  var img_map_id = YAHOO.util.Dom.getAttribute(img, "usemap");
  var original_map = YAHOO.util.Selector.query(img_map_id)[0];

  var area_nodes = YAHOO.util.Selector.query("area", original_map);
  var last_node = area_nodes[area_nodes.length - 1];
  for (var y = 0; y < row_count; y++) {
    var top = Math.round(plotarea_top + (y * tile_height));
    var bottom = Math.round(plotarea_top + (y * tile_height) +
        tile_height);
    for (var x = 0; x < column_count; x++) {
      var left = Math.round(plotarea_left + (x * tile_width));
      var right = Math.round(plotarea_left + (x * tile_width) +
          tile_width);
      var area = document.createElement("area");
      YAHOO.util.Dom.setAttribute(area, "shape", "rect");
      YAHOO.util.Dom.setAttribute(area, "onmouseover",
        "alert('This does not appear in IE')"
      );
      var coords = [
        left, top,
        right, bottom
      ];
      YAHOO.util.Dom.setAttribute(area, "coords", coords.join(","));
      YAHOO.util.Dom.insertBefore(area, last_node);
    }
  }
}

Where in the days of IE4 and NS4, IE was always the forgiving browser that swallowed all kinds of our little developing mistakes, it seems that at least for this IMG, MAP and AREA functionality the tables are turned. 在IE4和NS4的时代,IE总是宽容的浏览器,它吞噬了我们各种各样的小错误,似乎至少对于这个IMG,MAP和AREA功能来说,表格都是转向的。

Creating and adding an image with map with area dynamically via createElement and appendChild, FireFox works first time and IE not (Now end 2012 Win7 IE9!). 通过createElement和appendChild动态创建和添加带有地图的地图的图像,FireFox第一次工作而IE不工作(现在结束2012 Win7 IE9!)。

After all checking, tweaking and tuning IE still would not work. 毕竟检查,调整和调整IE仍然无法正常工作。 The only solution I could find that work for both FF and IE was by feeding innerHTML with what you'd normally type in the body. 我发现FF和IE工作的唯一解决方案是将innerHTML与你通常在体内输入的东西一起喂食。

I thought great! 我觉得很棒! at least solution that works for both, but a minute later I couldn't stand it, because I'm on this - by the book createElement and appendChild route - by with I like to consistently create documents. 至少解决方案适用于两者,但一分钟后我无法忍受,因为我在这 - 通过书籍createElement和appendChild路由 - 我喜欢一致地创建文档。

I started out with the createElement and appendChild code for FF next to innerHTML code for IE. 我开始使用IE的innerHTML代码旁边的FF的createElement和appendChild代码。 Then I moved the IMG tag from innerHTML to createElement and appendChild route. 然后我将IMG标签从innerHTML移动到createElement和appendChild路由。 It didn't work anymore for IE and started trying and trying. 它不再适用于IE并开始尝试和尝试。 Then I did the same for the other tags and after trying the whole afternoon, and eventually I discovered the complete but narrow path for IE. 然后我对其他标签做了同样的事情并且在整个下午尝试之后,最终我发现了IE的完整但狭窄的路径。

For image creation: 用于图像创建:

//USE THIS:
img = new Image(width, height);
//DOES NOT WORK:
img = document.createElement('IMG');
img.width = width;
img.height = height;
//DOES NOT WORK:
img = new Image();
img.width = width;
img.height = height;

further assign props like .src as usual. 像往常一样分配像.src这样的道具。

Use camel-case properties in script 在脚本中使用camel-case属性

//USE THIS:
img.useMap = '#myMap';
//DOES NOT WORK:
img.usemap = '#myMap';

HTML props are case in-sensitive, but in script they are case sensitive. HTML道具不区分大小写,但在脚本中它们区分大小写。

And this is an undocumented, very funny (not!), for MS just to stuppid bug/quirck, that is still in this IE9 after having the MAP tag build in since IE4. 这是一个没有文档记录,非常有趣(不是!),对于MS来说只是为了补充bug / quirck,从IE4开始构建MAP标签后仍然在IE9中。 Anyway... 无论如何...

//USE THIS:
map = document.createElement('MAP');
map.id = "myMap";
map.name = "myMap";
//DOES NOT WORK:
map = document.createElement('MAP');
map.name = "myMap";

In HTML IE uses the .name property, but in script IE uses the .id property. 在HTML中,IE使用.name属性,但在脚本IE中使用.id属性。 In case you use FF and IE code forking, I'd still keep the .name prop in the IE code in case MS fixes it. 如果您使用FF和IE代码分叉,我仍然会在IE代码中保留.name prop以防MS修复它。

Finally, here is a working piece of code for IE and FF using the createElement and appendChild route, yes!: 最后,这是使用createElement和appendChild路由的IE和FF的一段代码,是的!:

document.write('<INPUT type="button" value="Dynamic Image Map" onclick="dynamicImageMap(32, 64)">');
function dynamicImageMap(ox, oy)
{
    var a, b, map, img, div, s;

    a = document.createElement('AREA');
    a.shape = 'rect';
    a.coords = '128,0,250,128';
    a.noHref = true;

    b = document.createElement('AREA');
    b.shape = 'rect';
    b.coords = '0,128,128,250';
    b.noHref = true;

    map = document.createElement('MAP');
    map.id = "myMap";
    map.name = "myMap";
    map.onclick = function(){ alert('myMap'); }

    map.appendChild(a);
    map.appendChild(b);

    img = new Image(250, 250);
    img.src = 'http://www.vectortemplates.com/raster/batman-logo-big.gif';
    img.border = 0;
    img.useMap = '#myMap';

    div = document.createElement('DIV');

    s = div.style;
    s.position = 'absolute';
    s.left = ox +'px';
    s.top = oy +'px';
    s.width = '250px';
    s.height = '250px';
    s.overflow = 'hidden';
    s.visibility = 'inherit';

    div.appendChild(map);
    div.appendChild(img);

    document.body.appendChild(div);
}

The image map areas makes the lower-left and higher-right quarters of the image hot for clicking. 图像映射区域使图像的左下角和右上角四分之一处于热点状态。 Replace the image src with an existing image src in case it doesn't exist/work anymore. 将图像src替换为现有图像src,以防它不再存在/工作。

Goodluck and Have Phun! Goodluck和Phun!

Solved. 解决了。

I've concluded that internet explorer doesn't add appropriate event processing to the dynamically generated area elements. 我得出结论,Internet Explorer不会为动态生成的区域元素添加适当的事件处理。 Whereas the original area elements will fire mouse-over events, the dynamically added elements do not. 原始区域元素将触发鼠标悬停事件,而动态添加的元素则不会。 The following code adds mouse-over events to the dynamically added area elements: 以下代码将鼠标悬停事件添加到动态添加的区域元素:

if (YAHOO.env.ua.ie > 0) {
  YAHOO.util.Event.addListener(original_map, "mousemove", function (e) {
    var area = YAHOO.util.Event.getTarget(e);
    var mouseover = YAHOO.util.Dom.getAttribute(area, "onmouseover");
    if (mouseover !== null) {
      eval("(function() {" + mouseover + "})();");
    }
  });
}

The mouse-over events now fire as expected, and my dynamically generated image map behaves like a static one in IE. 鼠标悬停事件现在按预期触发,我动态生成的图像映射在IE中的行为类似于静态映像。

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

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