あなたが言及している特定のイベントハンドラーは、初期ウィジェット表示_create
の設定を担当するjQuery Mobileメソッドに含まれています。navbar
アプローチ 1:
イベント バインドを使用してmobileinit
、jQuery Mobile のデフォルト設定をオーバーライドできます。mobileinit イベントはすぐにトリガーされるため、jQuery Mobile が読み込まれる前にイベント ハンドラーをバインドする必要があります。イベント ハンドラでは、メソッドをオーバーライドできます$.mobile.navbar.prototype._create
。メソッドの元の実装は、navbar _create
言及しているコードとは別にコピーされます。そのコードはコメントアウトされています。
サンプル例:
<!DOCTYPE html>
<html>
<head>
<title>jQuery Mobile Override Function Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).bind("mobileinit", function(){
$.mobile.navbar.prototype._create = function() {
var $navbar = this.element,
$navbtns = $navbar.find( "a" ),
iconpos = $navbtns.filter( ":jqmData(icon)" ).length ?
this.options.iconpos : undefined;
$navbar.addClass( "ui-navbar ui-mini" )
.attr( "role","navigation" )
.find( "ul" )
.jqmEnhanceable()
.grid({ grid: this.options.grid });
$navbtns.buttonMarkup({
corners: false,
shadow: false,
inline: true,
iconpos: iconpos
});
/*
$navbar.delegate( "a", "vclick", function( event ) {
if( !$(event.target).hasClass("ui-disabled") ) {
$navbtns.removeClass( $.mobile.activeBtnClass );
$( this ).addClass( $.mobile.activeBtnClass );
}
});
*/
// Buttons in the navbar with ui-state-persist class should regain their active state before page show
$navbar.closest( ".ui-page" ).bind( "pagebeforeshow", function() {
$navbtns.filter( ".ui-state-persist" ).addClass( $.mobile.activeBtnClass );
});
};
});
</script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
</head>
<body>
<!-- /page -->
<div data-role="page">
<!-- /header -->
<div data-role="header" data-theme="b">
<h1>jQuery Mobile Override Example</h1>
</div>
<!-- /content -->
<div data-role="content">
<div data-role="navbar">
<ul>
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
アプローチ 2:
_create
メソッド全体をオーバーライドするには、次のコードを使用できます。この方法を使用して、元の実装をカスタマイズできます。
(function($){
// reference to the original method
var _old = $.mobile.navbar.prototype._create;
$.mobile.navbar.prototype._create = function() {
// put your custom code here
// .....
// in case you want to apply the default behaviour
// return _old.apply(this);
};
})(jQuery);
以下に実際の例を示します。メソッドの元の実装は、navbar _create
言及しているコードとは別にコピーされます。そのコードはコメントアウトされています。
<!DOCTYPE html>
<html>
<head>
<title>jQuery Mobile Override Function Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
<script>
(function($){
// store original reference to the method
var _old = $.mobile.navbar.prototype._create;
$.mobile.navbar.prototype._create = function() {
var $navbar = this.element,
$navbtns = $navbar.find( "a" ),
iconpos = $navbtns.filter( ":jqmData(icon)" ).length ?
this.options.iconpos : undefined;
$navbar.addClass( "ui-navbar ui-mini" )
.attr( "role","navigation" )
.find( "ul" )
.jqmEnhanceable()
.grid({ grid: this.options.grid });
$navbtns.buttonMarkup({
corners: false,
shadow: false,
inline: true,
iconpos: iconpos
});
/*
$navbar.delegate( "a", "vclick", function( event ) {
if( !$(event.target).hasClass("ui-disabled") ) {
$navbtns.removeClass( $.mobile.activeBtnClass );
$( this ).addClass( $.mobile.activeBtnClass );
}
});
*/
// Buttons in the navbar with ui-state-persist class should regain their active state before page show
$navbar.closest( ".ui-page" ).bind( "pagebeforeshow", function() {
$navbtns.filter( ".ui-state-persist" ).addClass( $.mobile.activeBtnClass );
});
};
})(jQuery);
</script>
</head>
<body>
<!-- /page -->
<div data-role="page">
<!-- /header -->
<div data-role="header" data-theme="b">
<h1>jQuery Mobile Override Example</h1>
</div>
<!-- /content -->
<div data-role="content">
<div data-role="navbar">
<ul>
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>