开发者

How to dynamically change the jQueryUI icon of a button after initialization?

开发者 https://www.devze.com 2023-01-31 06:04 出处:网络
<script> $(function() { $( \"#dynabutton\" ).button( { icons: { primary: \"ui-icon-gear\" }, text: false
<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>

On click of the #swap button, 开发者_Python百科I want to switch the icon (jQueryUI icon) associated with #dynabutton from ui-icon-gear to ui-icon-locked.

But I don't know if this is supported?


You can call .button("option", options) to set options later (like other jQuery UI widgets), including the icons:

$(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.

0

精彩评论

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

关注公众号