4

我有一个按钮,当它被点击时,我想用图像替换按钮。我怎样才能在 JQuery 中做到这一点?是否也可以替换图像的背景?按钮本身位于一个大 div 中,我不想在按钮周围添加另一个 div,因为它会弄乱以前的布局。

4

2 回答 2

8

如果要替换按钮元素:

$('the-button').bind('click', function () {
    $(this).replaceWith('<img src="/wherever.jpg"/>');
});

如果要更改按钮的背景图像:

$('the-button').bind('click', function () {
    $(this).css('backgroundImage', 'url(\'/wherever.jpg\')');
});

如果要更改图像的背景图像(:S):

$('the-button').bind('click', function () {
    $(this).replaceWith('<img src="/wherever.jpg" style="background-image:url(\'/somewhere-else.jpg\');"/>');
});
于 2010-03-26T02:27:22.037 回答
0

我不确定单击按钮时要做什么,但是:

$('button').click(function()
{
   $(this).css({ // properties you want to change });
});

应该做的伎俩。

于 2010-03-26T02:27:46.013 回答