从canvas的链接集调用JavaScript函数会打开一个带有错误的新选项卡
Calling a JavaScript function from a link set at canvas opens a new tab with an error
我试图在HTML5 Canvas的特定部分设置超链接。我希望点击该超链接调用一个Javascript函数,改变画布上的一些内容本身,但不打开任何新的浏览器选项卡或页面。然而,我得到的是(I)画布内容实际上被我调用的函数改变了,但是(ii)我得到一个新的浏览器选项卡,显示错误ERR_EMPTY_RESPONSE。我怎样才能解决这个问题呢?提前感谢!
我的代码是(我使用的是几年前'dogbane'在这里发布的"画布上的链接"解决方案的变体:
// this function is called from WITHIN another function (print_meteosarriaYearMonthData) which contains the variable declarations
function draw_link(ctx)
{
//add mouse listeners
canvas.addEventListener("mousemove", on_mousemove, false);
canvas.addEventListener("click", on_click, false);
}
//check if the mouse is over the link and change cursor style
function on_mousemove (ev) {
var x, y;
// Get the mouse position relative to the canvas element.
if (ev.layerX || ev.layerX == 0) { //for firefox
x = ev.layerX;
y = ev.layerY;
}
x-=canvas.offsetLeft;
y-=canvas.offsetTop;
//is the mouse over the link?
if(x>=linkX && x <= (linkX + linkWidth) && y<=linkY && y>= (linkY-linkHeight)){
document.body.style.cursor = "pointer";
inLink=true;
}
else{
document.body.style.cursor = "";
inLink=false;
}
}
//if the link has been clicked, go to link
function on_click(e) {
if (inLink) {
// this function is where the link-related functions are embedded and does the job of changing the canvas contents (depending on the parameter METEODATA_MONTH or METEODATA_YEAR
print_meteosarriaYearMonthData(METEODATA_MONTH);
return false; // is this necessary?
}
}
print_meteosarriaYearMonthData的代码为
function print_meteosarriaYearMonthData(yearOrMonth)
{
var canvas = document.getElementById("meteoinfo");
var ctx = canvas.getContext("2d");
var linkWidth;
var linkHeight;
var linkX;
var linkY;
if (yearOrMonth == METEODATA_YEAR)
{
// PRINT YEAR
<...code to pint year>
// Print tab & dimmed month with link
<further code to print data on canvas>
linkWidth = ctx.measureText(month+"/"+year).width;
linkHeight = 30;
linkX = meteoYearDataYearAnchorX+50;
linkY = meteoYearDataYearAnchorY-20;;
draw_link(ctx); // CALL TO DRAW LINK FUNCTION
// Get yearly data
<code to get & print yearly data >
}
else
{
// PRINT MONTH
<code to print current month>
}
// start of draw_link, on_mousemove & on_click functions code
function draw_link(ctx)
{
...
}
function on_mousemove(ctx)
{
...
}
function on_click(ctx)
{
...
}
}
别管这个问题了。代码没问题。显然,我正在使用存储在缓存中的Javascript文件的先前错误版本。现在运行正常。
请原谅给您带来的不便,并感谢所有可能看过这篇帖子并试图找到我所描述行为原因的人(特别是"somethinghere")。
对
相关文章:
- 如果sql查询成功,请打开一个新选项卡
- 在私人浏览中打开一个新选项卡
- 当主窗口是 chrome 时是否可以在 IE 中打开一个新选项卡 |ASP.Net
- 我想打开一个新选项卡而不是弹出窗口
- 打开一个新选项卡以在 AngularJS 中发出 http 请求
- 在按钮上单击弹出窗口并打开一个新选项卡窗口并在 JavaScript 中专注于它
- 在jQuery中创建的URL既重定向到又打开一个新选项卡
- 使用Javascript执行一个函数或打开一个新选项卡
- 模拟IFrame中的单击,以便打开一个新选项卡
- 如何在不制作另一个新选项卡或新浏览器窗口的情况下在同一网页中播放MPG视频/音频文件
- 是否可以在chrome的检查元素窗口中添加一个新选项卡
- 使用自定义html代码在浏览器中打开一个新选项卡.Javascript
- 如何在第二个位置添加一个新选项
- 如何打开一个新选项卡并在新选项卡上运行脚本后立即关闭它
- Firefox打开一个新选项卡,而不是javascript代码
- 如何在CasperJS中打开一个新选项卡
- 强制浏览器打开一个新选项卡
- 打开一个新选项卡,而不是一个新窗口
- 为什么这行代码没有打开一个新选项卡?
- 使用javascript打开一个新选项卡