开发者

Swap button for image (Jquery)

开发者 https://www.devze.com 2022-12-25 04:09 出处:网络
I have a button and when it\'s clicked, I want to replace the button with an imag开发者_Python百科e. How can I do this in JQuery? Is it possible to replace the background of the image as well? The but

I have a button and when it's clicked, I want to replace the button with an imag开发者_Python百科e. How can I do this in JQuery? Is it possible to replace the background of the image as well? The button itself is within a large div, and I don't want to add another div around the button because it messes up a previous layout.


If you want to replace the button element:

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

If you want to change the background image of the button:

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

If you want to change the background image of the image (:S):

$('the-button').bind('click', function () {
    $(this).replaceWith('<img src="/wherever.jpg" style="background-image:url(\'/somewhere-else.jpg\');"/>');
});


I am not exactly sure what you want to do when the button is clicked, but:

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

Should do the trick.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号