将图像添加到 jquery 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11141607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:14:23  来源:igfitidea点击:

Add an image to the jquery button

jqueryjquery-uijquery-ui-button

提问by user525146

I am wondering how to add an image to the jquery button. Till now I was using the jquery ui button in my project and I got custom images from the graphics team, so I would like to implement them.

我想知道如何将图像添加到 jquery 按钮。直到现在我在我的项目中使用 jquery ui 按钮,我从图形团队获得了自定义图像,所以我想实现它们。

submit.button({ icons: {primary:'ui-icon-trash'}, text: false })

How to add custom image to this jquery button?

如何将自定义图像添加到这个 jquery 按钮?

回答by Dzeimsas Zvirblis

I would just assign a class to the button...

我只想为按钮分配一个类......

$("#buttonId").button({

    icons: {primary: null},
    text: false

}).addClass("ButtonClass");

Then your CSS class could look something like this...

那么你的 CSS 类可能看起来像这样......

.ButtonClass
{
    background-image: url(../images/Button.png);   
    background-repeat: no-repeat; 
    border: none;    
}

回答by TJ VanToll

<button>Trash</button>
<style>
    .ui-button ?.ui-button-icon-primary {
        background: url('/my-custom-trash-image.png');
    }?
</style>
<script>
    $(function() {
        $('button').button({
            icons: {
                primary: 'ui-icon-trash'
            },
            text: false
        });
    });
</script>

Unfortunately you do have to pass in a valid value for the primaryicon even though it won't be used.

不幸的是,您必须为primary图标传递一个有效值,即使它不会被使用。

Live Example- http://jsfiddle.net/aMVrz/

现场示例- http://jsfiddle.net/aMVrz/