初始化后如何动态更改按钮的jQueryUI图标?

时间:2010-12-15 15:21:46

标签: jquery jquery-ui jquery-ui-button

<script>
    $(function() 
    {
        $( "#dynabutton" ).button(
        {
            icons: 
            {
                primary: "ui-icon-gear"
            },
            text: false
        });

        $( "#swap" ).button(
        {
            icons: 
            {
                primary: "ui-icon-locked"
            },
            text: true
        }).click(function(event)
        {
            // change #dynabutton icon from
            // "ui-icon-gear"
            // to:
            // "ui-icon-locked"
        });         
    });
    </script>



<div class="demo">

<button id="dynamic_button">Button with gear icon</button>
<button id="swap">Swap icons</button>

</div>

点击#swap按钮,我想将与#dynabutton相关联的图标(jQueryUI icon)从ui-icon-gear切换为ui-icon-locked

但我不知道这是否得到支持?

1 个答案:

答案 0 :(得分:22)

您可以稍后调用.button("option", options)来设置选项(与其他jQuery UI小部件一样),包括图标:

$(function() {
    $( "#dynabutton" ).button({
        icons: { primary: "ui-icon-gear" },
        text: false
    });
    $( "#swap" ).button({
        icons: { primary: "ui-icon-locked" },
        text: true
    }).click(function() {
        $( "#dynabutton" ).button("option", {
          icons: { primary: "ui-icon-locked" }
        });
    });         
});

You can test it here