比方说,我有#button与给定的background-image:url(images/some_background),我想改变它到另一个背景,当我点击它,让我们说url(图像/ other_background)。更改按钮背景颜色点击本身
HTML
<a id="button" ></a>
CSS
display: block;
height:100px;
width:100px;
background-image:url(images/other_background.png)
我尝试这样做,但它不工作:
$(document).ready(function(){
$('#button').click(function(){
$('#button').css('backgroundImage', 'url(images/other_background.png)');
});
});
我认为他遇到的具体问题与他有关,并不妨碍他的超链接默认事件。在对一个被删除的答案发表评论时,他表示他将它改为了一个按钮,并且工作。 – djlumley
上面的评论钉了它。回到我的原始代码,它神奇地工作。还有一个问题,我将如何去永久更改按钮的背景图像,所以如果我重新加载页面,“other_background”会在那里。 – user461316
您可以使用HTML5本地存储(http://diveintohtml5.org/storage.html)来存储状态,然后在页面加载时查询该状态? – djlumley