onmouse活动在Chrome应用程序中无效

时间:2015-07-09 10:01:53

标签: javascript javascript-events mouseevent google-chrome-devtools google-chrome-app

我创建了这段代码:



window.onload = function() {


  for (var i = 10; i < 100; i += 10) {
    document.getElementById("overlayInner").innerHTML += '<div style="top:' + ((8 * i) - 15) + 'px;" class="number lat lat' + i + '">' + i + '</div>';
    document.getElementById("overlayInner").innerHTML += '<span style="left:' + ((8 * i) - 15) + 'px;" class="number lon lon' + i + '">' + i + '</span>';
  }

  var map_canvas = document.getElementById("map");
  var context = map_canvas.getContext("2d");

  for (var x = 80; x < 800; x += 80) {
    context.moveTo(x, 45);
    context.lineTo(x, 755);
  }

  var count = 1;
  for (var y = 80; y < 800; y += 80) {
    if (count != 10) {
      context.moveTo(50, y);
      context.lineTo(750, y);
    }
  }
  context.strokeStyle = "#7c7c7c";
  context.stroke();

};


function move(id, evt) {
  var e = document.getElementById(id);
  e.style.display = "block";
  e.style.left = evt.pageX + 10 + "px";
  e.style.top = evt.pageY + 10 + "px";

  e.innerHTML = "X: " + Math.round(((evt.pageX * 1.25) / 10)) + " / Y: " + Math.round(((evt.pageY * 1.25) / 10));
}

function hide(id) {
  document.getElementById(id).style.display = "none";
}
&#13;
/* CSS file placeholder. */

body {
  padding: 0;
  margin: 0;
  background: #313335;
}
#map_container {
  position: relative;
}
#map {
  width: 800px;
  height: 800px;
}
#overlay {
  width: 800px;
  height: 800px;
  position: absolute;
  top: 0;
  left: 0;
}
#overlay:hover {
  cursor: crosshair;
}
#overlayInner {
  width: 100%;
  height: 100%;
  display: block;
  position: relative;
}
.number {
  background: white;
  padding: 5px;
  border: 2px solid black;
  position: absolute;
}
.lat {
  left: 25px;
}
.lon {
  bottom: 25px;
}
canvas {
  border: 1px solid black;
  position: relative;
}
canvas:hover {
  cursor: crosshair;
}
#coordinates {
  position: absolute;
  color: white;
  display: none;
}
&#13;
<canvas id="map" width="800px" height="800px"></canvas>
<div id="overlay" onmousemove="move('coordinates',event)" onmouseout="hide('coordinates')">
  <div id="overlayInner"></div>
</div>
<span id="coordinates"></span>
&#13;
&#13;
&#13;

当我通过堆栈代码运行它或者当我将其作为网站运行时,它似乎在Chrome浏览器中工作。但是,当我将其作为Chrome应用运行时,我没有坐标。

我是Chrome应用的新手,所以我不确定是否有可能实现这一目标。但是,我想我会问是否需要以不同的方式编写,也许我的代码中存在浏览器在运行时修复的错误?这段代码只是标准的Javascript,所以我无法理解为什么它不起作用。

1 个答案:

答案 0 :(得分:1)

Chrome扩展程序不允许使用内联事件处理程序

onmousemove="move('coordinates',event)" onmouseout="hide('coordinates')"

以下代码可以解决问题。

eO = document.getElementById('overlay');

eO.addEventListener("mousemove", function(evt){
   move('coordinates', evt);
});

eO.addEventListener("mouseout", function(){
    hide('coordinates');
});