JS更改存储在变量中的图像的光标
是否可以使用存储在变量中的自定义图像更改光标?
我尝试过setAttribute或.pstyle.cursor,但它似乎不起作用。.
以下是我尝试过的一些示例:
var MyImage = MyImage.png;
document.getElementById("DivWithMouseOverEvent").style.cursor = MyImage;
我也尝试过:
var MyImage = MyImage.png;
document.getElementById("DivWithMouseOverEvent").setAttribute("cursor", MyImage);
想法是,var myimage可能会根据用户的某些操作而改变。
我知道Firefox需要第二个光标选项,但我不知道如何使用变量进行操作。
Is it possible to change the cursor with a customized image stored in a variable?
I have tried setAttribute or .style.cursor, but it does not seems to work..
Here are some examples of what I have tried:
var MyImage = MyImage.png;
document.getElementById("DivWithMouseOverEvent").style.cursor = MyImage;
I also tried:
var MyImage = MyImage.png;
document.getElementById("DivWithMouseOverEvent").setAttribute("cursor", MyImage);
The idea is that the var MyImage may change depending on some actions by the user.
I know Firefox requires a second cursor option, but I do not know how to do it with a variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
给出的代码有两个问题。
图像定义不仅需要是字符串,还需要一个URL。
另外,必须有后备(不仅是Firefox所要求的)。
参见 mdn 有关允许的内容的更多详细信息。
There are a couple of problems with the code given.
The image definition needs to be not only a string, but also a url.
Also, a fallback is mandatory (not just required by Firefox).
See MDN for more detail on what is allowed.
这是我的例子。希望它可以帮助您
This is my example. Hope it can help you
------- 只需直接运行此代码即可确认
-------Simply run this code direct to confirm please