【发布时间】:2013-02-27 06:00:54
【问题描述】:
在elme被增强为Widget之后,jQuery mobile会插入很多元素。
例如,“a”将在“a”之后插入两个跨度元素,以增强为按钮。 那么,将“a”增强为按钮后,如何正确修改a的innerText呢?
【问题讨论】:
-
一些代码将帮助您提示您想要实现的目标,请:]
标签: jquery-mobile jquery-plugins jquery
在elme被增强为Widget之后,jQuery mobile会插入很多元素。
例如,“a”将在“a”之后插入两个跨度元素,以增强为按钮。 那么,将“a”增强为按钮后,如何正确修改a的innerText呢?
【问题讨论】:
标签: jquery-mobile jquery-plugins jquery
第一个官方方法不存在,所以必须手动完成。
常用的方法有两种:
$(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);
【讨论】: