我有一个动态页面内的选项卡。当用户按下选项卡时,选项卡很好用,但我想添加一个滑动功能,以便用户也可以滑动到下一个选项卡。
这是我尝试使滑动功能工作的方式:
function goToMatchDetailPage(matchHome, matchAway){
first_part_id = matchHome.substring(0,2);
sec_part_id = matchAway.substring(0,2);
var id = first_part_id.concat(sec_part_id);
//create the html template
var matchPage = $("<div data-role='page' data-url=dummyUrl><div data-role='header'><h1>"
+ matchHome + "</h1></div><div data-role='content'><div data-role='tabs'>"
+ "<div data-role='navbar'>"
+ "<ul>"
+ "<li><a href='#fragment-1'>" + matchHome + "</a></li>"
+ "<li><a href='#fragment-2'>" + matchAway + "</a></li>"
+ "</ul>"
+ "</div>"
+ "<div id='fragment-1'>"
+ "<p>This is the content of the tab 'One', with the id fragment-1.</p>"
+ "</div>"
+ "<div id='fragment-2'>"
+ "<p>This is the content of the tab 'Two', with the id fragment-2.</p>"
+ "</div></div></div>");
//append the new page to the page contanier
matchPage.appendTo($.mobile.pageContainer);
//go to the newly created page
$.mobile.changePage(matchPage);
这里是不起作用的部分。
$(function(){
// Bind the swipeleftHandler callback function to the swipe event on div.box
$( "div" ).on( "swipeleft", swipeleftHandler );
// Callback function references the event target and adds the 'swipeleft' class to it
function swipeleftHandler( event ){
//go to the newly created page
$.mobile.changePage('#fragment-2');
}
});
}
$( "div" ).bind( "swipeleft", swipeleftHandler );
吗? - sharshidocument.ready
函数是一个坏主意,请查看事件文档:http://demos.jquerymobile.com/1.2.1/docs/api/events.html。当您创建新元素时,您的初始绑定将不会影响这些元素,因此您必须重新绑定到新元素或使用委托事件处理程序。关于此问题在Stack Overflow上有大量信息。 - Jasper