Javascript jQuery Mobile:动态添加内容的标记增强
我想知道如何动态增强jQuery移动页面 我曾尝试使用以下方法:Javascript jQuery Mobile:动态添加内容的标记增强,javascript,jquery,html,cordova,jquery-mobile,Javascript,Jquery,Html,Cordova,Jquery Mobile,我想知道如何动态增强jQuery移动页面 我曾尝试使用以下方法: $('[data role=“page”]')。触发器('create') 及 $('[data role=“page”]')。page() 另外,如何防止仅对复选框进行增强标记?免责声明: 这篇文章也可以在我的博客中找到 简介: 有几种方法可以增强动态创建的内容标记。仅仅向jQuery Mobile页面动态添加新内容是不够的,新内容必须使用经典的jQuery Mobile样式进行增强。由于这是一项相当繁重的任务,因此需要确定
$('[data role=“page”]')。触发器('create')代码>
及
$('[data role=“page”]')。page()代码>
$('#mylist').listview('refresh');
$('[type="button"]').button();
$('[data-role="navbar"]').navbar();
$('[type="text"]').textinput();
$('[type="range"]').slider();
$('[type="radio"]').checkboxradio();
$('select').selectmenu();
$('.selector').trigger('pagecreate');
$(document).bind('mobileinit',function(){
$.mobile.page.prototype.options.keepNative = "select, input";
});
删除listview元素:
$('#mylist li').eq(0).addClass('ui-screen-hidden');
增强示例:
请注意,refresh()方法仅影响附加到列表的新节点。这样做是出于性能原因
listview的一个亮点是过滤功能。不幸的是,由于某些原因,jQuery Mobile将无法向现有listview动态添加筛选器选项。幸运的是,有一个解决办法。如果可能,请删除当前listview,并在启用文件管理器选项的情况下添加另一个listview
下面是一个工作示例:
增强示例:
还有一件事,您不需要使用输入元素来创建按钮,甚至可以使用基本div来创建按钮,下面是一个示例:$('#mylist').listview('refresh');
$('[type="button"]').button();
$('[data-role="navbar"]').navbar();
$('[type="text"]').textinput();
$('[type="range"]').slider();
$('[type="radio"]').checkboxradio();
$('select').selectmenu();
$('.selector').trigger('pagecreate');
$(document).bind('mobileinit',function(){
$.mobile.page.prototype.options.keepNative = "select, input";
});
增强示例:
下面是如何添加动态导航栏选项卡的演示:
在创建前的页面中还有一个事件:$('#mylist').listview('refresh');
$('[type="button"]').button();
$('[data-role="navbar"]').navbar();
$('[type="text"]').textinput();
$('[type="range"]').slider();
$('[type="radio"]').checkboxradio();
$('select').selectmenu();
$('.selector').trigger('pagecreate');
$(document).bind('mobileinit',function(){
$.mobile.page.prototype.options.keepNative = "select, input";
});
增强示例:$('#mylist').listview('refresh');
$('[type="button"]').button();
$('[data-role="navbar"]').navbar();
$('[type="text"]').textinput();
$('[type="range"]').slider();
$('[type="radio"]').checkboxradio();
$('select').selectmenu();
$('.selector').trigger('pagecreate');
$(document).bind('mobileinit',function(){
$.mobile.page.prototype.options.keepNative = "select, input";
});
增强示例:
pagebeforecreate事件期间的增强示例:
滑块的动态创建有点麻烦,请在此处阅读更多信息:$('#mylist').listview('refresh');
$('[type="button"]').button();
$('[data-role="navbar"]').navbar();
$('[type="text"]').textinput();
$('[type="range"]').slider();
$('[type="radio"]').checkboxradio();
$('select').selectmenu();
$('.selector').trigger('pagecreate');
$(document).bind('mobileinit',function(){
$.mobile.page.prototype.options.keepNative = "select, input";
});
或者,如果要选择/取消选择另一个Radiobox/Checkbox元素:
$("input[type='radio']").eq(0).attr("checked",false).checkboxradio("refresh");
或
增强示例:$('#mylist').listview('refresh');
$('[type="button"]').button();
$('[data-role="navbar"]').navbar();
$('[type="text"]').textinput();
$('[type="range"]').slider();
$('[type="radio"]').checkboxradio();
$('select').selectmenu();
$('.selector').trigger('pagecreate');
$(document).bind('mobileinit',function(){
$.mobile.page.prototype.options.keepNative = "select, input";
});
增强示例:$(".selector").table("refresh");
虽然这是一种标准的表格增强方法,但在这一点上,我无法让它工作。所以改用触发器('create')
增强示例:$('#mylist').listview('refresh');
$('[type="button"]').button();
$('[data-role="navbar"]').navbar();
$('[type="text"]').textinput();
$('[type="range"]').slider();
$('[type="radio"]').checkboxradio();
$('select').selectmenu();
$('.selector').trigger('pagecreate');
$(document).bind('mobileinit',function(){
$.mobile.page.prototype.options.keepNative = "select, input";
});
动态添加到面板的内容的标记增强:
$('.selector').trigger('pagecreate');
例如:$('#index').trigger('create');
增强示例:
增强整页内容(页眉、内容、页脚):
不幸的是,触发器('create')不能增强页眉和页脚标记。在这种情况下,我们需要大炮:
$('#index').trigger('pagecreate');
增强示例:
这几乎是一种神秘的方法,因为我在官方jquerymobile文档中找不到它。尽管如此,在jquerymobile的bug跟踪器中还是可以很容易地找到它,并警告说除非确实需要,否则不要使用它
注意,.trigger('pagecreate')假设每页刷新只能使用一次,我发现它不真实:
第三方增强插件
有几个第三方增强插件。有些是对现有方法的更新,有些是为了修复损坏的jQM功能
- 按钮文本更改
不幸的是,我找不到这个插件的开发者。原始资料来源:
下面是一个使用谷歌地图api3的工作示例:
此方法可用于获得正确的最大内容高度,并且必须与pageshow事件一起使用
到页眉、内容、页脚容器 这也需要在应用程序加载阶段打开:function getRealContentHeight() { var header = $.mobile.activePage.find("div[data-role='header']:visible"); var footer = $.mobile.activePage.find("div[data-role='footer']:visible"); var content = $.mobile.activePage.find("div[data-role='content']:visible:visible"); var viewport_height = $(window).height(); var content_height = viewport_height - header.outerHeight() - footer.outerHeight(); if((content.outerHeight() - header.outerHeight() - footer.outerHeight()) <= viewport_height) { content_height -= (content.outerHeight() - content.height()); } return content_height; }
在初始化jquery-mobile.js之前初始化它(参见下面的示例)。 有关这方面的更多信息,请参见: 例如: 要重新创建页面,请使用以下命令:$(document).one("mobileinit", function () { $.mobile.ignoreContentEnabled=true; });
$('#index').live('pagebeforeshow', function (event) { $.mobile.ignoreContentEnabled = false; $(this).attr('data-enhance','true'); $(this).trigger("pagecreate") });
- 方法2: 第二个选择是做我自己