当光标悬停在HTML5画布上的图像上时,更新段落中显示的文本
Updating the text displayed in a paragraph when the cursor hovers over an image on HTML5 canvas
我在HTML5画布上有一些图像,我想调用一个函数,每当光标悬停在其中一个图像上时,该函数就会更新HTML <p></p>
标记中显示的文本。使用以下功能将图像绘制到画布上:
function drawBox(imageObj, img_x, img_y, img_width, img_height) {
console.log("function drawBox is drawing the description boxes");
var canvasImage = new Kinetic.Image({
image: imageObj,
width: img_width,
height: img_height,
// puts the image in the middle of the canvas
x: img_x,
y: img_y
});
// add cursor styling
imagesLayer.add(canvasImage);
}
我已经尝试向每个图像添加一个onmouseover
函数,方法是添加一些代码来调用我所拥有的将段落内容更新到此函数的函数,因此我的drawBox
函数现在看起来如下:
function drawBox(imageObj, img_x, img_y, img_width, img_height) {
console.log("function drawBox is drawing the description boxes");
var canvasImage = new Kinetic.Image({
image: imageObj,
width: img_width,
height: img_height,
// puts the image in the middle of the canvas
x: img_x,
y: img_y
});
// add cursor styling
canvasImage.on("mouseover", function(){
//displayAssetDescriptionTip();
console.log("displayAssetDescriptionTip() is being called on mouseover event");
});
imagesLayer.add(canvasImage);
}
当我如上所述使用该函数查看页面,并将光标悬停在已添加该函数的图像上时,我添加到mouseover
函数中的日志将显示在控制台中,这样我就知道鼠标悬停工作正常。
但是,我遇到的问题是要从mouseover
事件中调用的函数。您将在上面看到,我已经在mouseover
事件中注释掉了对函数displayAssetDescriptionTip()
的调用。这是我想在光标悬停在这些图像上时调用的函数,然而,它似乎总是将段落中的文本更改为属于第一个描述框的文本。。。即,如果光标悬停在我添加了mouseover
事件的任何图像上,则文本将更改为属于第一个图像的文本,而不是光标悬停的图像。
功能是:
function displayAssetDescriptionTip(){
if(canvasImage.id = "assetBox")
document.getElementById('tipsParagraph').innerHTML = tips[34];
else if(canvasImage.id = "liabilitiesBox")
document.getElementById('tipsParagraph').innerHTML = tips[35];
else if(canvasImage.id = "incomeBox")
document.getElementById('tipsParagraph').innerHTML = tips[36];
else if(canvasImage.id = "expenditureBox")
document.getElementById('tipsParagraph').innerHTML = tips[37];
else return;
console.log("displayAssetDescriptionTip being called");
}
有人知道为什么它总是将文本更改为第一个图像的文本,而不是与任何其他图像对应的文本吗?我已经设置它,根据光标悬停在哪个图像上,将文本更改为数组的不同元素,因此它应该为每个图像显示与该数组不同的元素。。。
在函数displayAssetDescriptionTip中,您使用的是=赋值运算符,而不是==比较运算符,因此您的第一个if条件总是匹配的。
更正的动作:
function displayAssetDescriptionTip(canvasImage){
if(canvasImage.id == "assetBox")
document.getElementById('tipsParagraph').innerHTML = tips[34];
else if(canvasImage.id == "liabilitiesBox")
document.getElementById('tipsParagraph').innerHTML = tips[35];
else if(canvasImage.id == "incomeBox")
document.getElementById('tipsParagraph').innerHTML = tips[36];
else if(canvasImage.id == "expenditureBox")
document.getElementById('tipsParagraph').innerHTML = tips[37];
else return;
console.log("displayAssetDescriptionTip being called");
}
不要忘记将canvasImage作为参数传递给事件处理程序
canvasImage.on("mouseover", function(){
displayAssetDescriptionTip(canvasImage);
});
相关文章:
- Javascript-在文本区域中断,但不在段落中中断
- 使用它更新集合中的嵌套数组's索引
- 正在更新mongod中两个对象内部的数组
- 可以't更新数据库中的对象
- 如何在javascript中更改段落中不同行的css样式
- 动态更新chartJ中的图表
- 使用AJAX更新Shopify中的购物车编号
- 更新Aurelia中二维数组的视图
- 如何通过代码更新github中托管的文件
- 为什么在ASP.NET Ajax更新面板中执行任何操作后javascript元素都停止工作
- 从一组具有相同起始和结束的所有段落的段落中提取包含特定字符串的段落
- 我怎么能在同一个HTML页面上多次在不同的段落中添加相同的字符串呢
- 如何等到动态内容加载到段落中
- 如何按从当前单词到段落中该句子(.)结尾的范围选择文本
- 如何更新文件中的 JSON 数据
- Angularjs 更新 setTimeout 中的范围变量不起作用
- 基于其他数据结构更新 AngularJS 中的数据结构
- 使用服务更新 AngularJS 中的控制器
- 单击“段落到文本区域”后,它会更新段落中的文本
- 当光标悬停在HTML5画布上的图像上时,更新段落中显示的文本