【问题标题】:Change button theme when pressed按下时更改按钮主题
【发布时间】:2011-12-18 21:13:28
【问题描述】:

我正在尝试更改按下按钮时的数据主题。

我在按钮的 onClick 处理程序中调用这个 JavaScript 函数:

function changeNextButtonColor() {
    var nextButton = document.getElementById('next');
    nextButton.setAttribute('data-theme', 'a');
}

但主题没有改变。有什么想法吗?

【问题讨论】:

  • 您的代码有效;其他事情出了问题。 JavaScript 控制台(Web Inspector、Firebug、Dragonfly...)说什么?是否报告了任何错误?
  • 谢谢大家!也许因为按钮是输入提交而不起作用?

标签: javascript jquery button jquery-mobile


【解决方案1】:

一旦你在按钮上设置了主题,你需要像下面这样调用它的“刷新”...

$("#next").attr("data-theme","a").button('refresh');

如果您想将这种行为绑定到进程中的所有“下一步”按钮并且它们可能不止一个,那么您可能需要考虑做更多类似的事情。它将检查每个页面是否有一个具有 colorChangeButton 类的按钮,然后在单击时将主题更改为在 data-theme-pressed 的按钮属性上指定的备用主题。

<a href="#" data-role="button" class="colorChangeButton" data-theme-pressed="a" data-theme="b">Next</a>

<script type='text/javascript'>
    $('div').live('pageinit', function(){
        $("a.colorChangeButton).click(function(){
           var $thisButton = $(this);
           $thisButton.attr("data-theme",$thisButton.attr("data-theme-pressed")).button('refresh');
        });
    });
</script>

【讨论】:

    【解决方案2】:
    function changeNextButtonColor() {
        $('#next').attr({'data-theme': 'a'});
    }
    

    如果您使用的是 jQuery,这是您的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多