我想动态设置按钮的背景图片。 (不使用 CSS 类)。
我试图通过设置按钮的 style.backgroundImage
属性来实现这一点,如下所示。 getOnMouseOverImage
绑定(bind)到 onmouseover
事件,按钮作为 this
传递。
myapp.ui.Button.prototype.getOnMouseOverImage = function(event) {
event.target.style.backgroundImage = "url( \'" + myapp.core.ImageStore.getInstance().getImageURL(this.buttonId) + "\')'"
event.preventDefault();
event.stopPropagation();
}
上面的代码似乎没有改变按钮的 backgroundImage
属性。我如何实现这一点?
最佳答案
在我看来你有一个额外的'
event.target.style.backgroundImage = "url( \'" + myapp.core.ImageStore.getInstance().getImageURL(this.buttonId) + "\')**'**"
我觉得应该是这样的
event.target.style.backgroundImage = "url( \'" + myapp.core.ImageStore.getInstance().getImageURL(this.buttonId) + "\')"
不确定这是否是问题所在,但由于我无法发表评论,所以我将发布此作为答案。
编辑:事实上,我什至不确定您是否需要撇号
event.target.style.backgroundImage = "url(" + myapp.core.ImageStore.getInstance().getImageURL(this.buttonId) + ")"
关于javascript - 在鼠标悬停时设置背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29156874/