【问题标题】:How to change jquery mobile button text如何更改jquery移动按钮文本
【发布时间】:2013-02-27 06:00:54
【问题描述】:

在elme被增强为Widget之后,jQuery mobile会插入很多元素。

例如,“a”将在“a”之后插入两个跨度元素,以增强为按钮。 那么,将“a”增强为按钮后,如何正确修改a的innerText呢?

【问题讨论】:

  • 一些代码将帮助您提示您想要实现的目标,请:]

标签: jquery-mobile jquery-plugins jquery


【解决方案1】:

第一个官方方法不存在,所以必须手动完成。

常用的方法有两种:

经典解决方案:

    $(document).on('pagebeforeshow', '#index', function(){       
        $('#custom-btn span span').text('This is a new text');
    });

现场示例:http://jsfiddle.net/Gajotres/EjVfz/

此解决方案预计未来的按钮实现不会发生任何变化,一切都将保持不变。

谨慎解决:

    $(document).on('pagebeforeshow', '#index', function(){       
        $('#custom-btn').find('.ui-btn-text').text('This is a new text');
    });

现场示例:http://jsfiddle.net/Gajotres/QHg3w/

此解决方案是一个安全的解决方案,无论使用哪个按钮示例(a、按钮、输入),按钮文本都将始终位于 .ui-btn-text 类中。

编辑:

更改按钮插件

$(document).on('pagebeforeshow', '#index', function(){       
    $('#custom-btn').changeButtonText('This is a new text');
});

(function($) {
    /*
     * Changes the displayed text for a jquery mobile button.
     * Encapsulates the idiosyncracies of how jquery re-arranges the DOM
     * to display a button for either an <a> link or <input type="button">
     */
    $.fn.changeButtonText = function(newText) {
        return this.each(function() {
            $this = $(this);
            if( $this.is('a') ) {
                $('span.ui-btn-text',$this).text(newText);
                return;
            }
            if( $this.is('input') ) {
                $this.val(newText);
                // go up the tree
                var ctx = $this.closest('.ui-btn');
                $('span.ui-btn-text',ctx).text(newText);
                return;
            }
        });
    };
})(jQuery);

现场示例:http://jsfiddle.net/Gajotres/mwB22/

解决方案/插件位置:https://stackoverflow.com/a/7279843/1848600

【讨论】:

  • 不优雅,但似乎我们必须这样做。
  • 远非优雅,但目前唯一的方法是使用 jQuery 手动完成。第二种解决方案更好。
  • 我发现你是一个第三方插件,看看我编辑的答案。
  • 谢谢你,它更优雅!我认为 jQuery mobile 需要一个 API 来修改增强的 DOM。
猜你喜欢
  • 1970-01-01
  • 2012-05-24
  • 2016-10-02
  • 2013-04-15
  • 2011-04-29
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
相关资源
最近更新 更多