Jquery mobile 为动态创建的jQuery移动页面附加事件处理程序

Jquery mobile 为动态创建的jQuery移动页面附加事件处理程序,jquery-mobile,Jquery Mobile,在我的jQuery移动应用程序中,我正在创建一些动态页面 function createPages() { $header = "<header data-role='header'><h1>Dynamically created pages</h1></header>"; $content = "<div data-role='content' class='content'><p>This is a dy

在我的jQuery移动应用程序中,我正在创建一些动态页面

function createPages()
{
    $header = "<header data-role='header'><h1>Dynamically created pages</h1></header>";
    $content = "<div data-role='content' class='content'><p>This is a dynamically generated page</p></div>";
    $footer = "<div data-role='footer'><h1>Audimax</h1></footer>";

    for($i=1;$i<=5;$i++)
    {   
        $section= "<section id='"+"#fav"+$i+"' data-role='page' data-url='"+"fav"+$i+"' class='dynamic'>";
        $new_page = $($section+$header+$content+$footer+"</section>");
        $new_page.appendTo($.mobile.pageContainer);

    }
}
函数createPages()
{
$header=“动态创建的页面”;
$content=“这是一个动态生成的页面

”; $footer=“Audimax”;
对于($i=1;$i为什么不在向DOM添加新的伪页面时绑定事件处理程序

function pageShowFunction () {
    console.log(this.id + ' has triggered the `pageShow` event!');
}
function createPages()
{
    $header = "...";
    $content = "...";
    $footer = "...";

    for($i=1;$i<=5;$i++)
    {   
        $section= "<section id='"+"#fav"+$i+"' data-role='page' data-url='"+"fav"+$i+"' class='dynamic'>";
        $new_page = $($section+$header+$content+$footer+"</section>").bind('pageshow', pageShowFunction);
        $new_page.appendTo($.mobile.pageContainer);

    }
}
function页面showfunction(){
console.log(this.id+'已触发'pageShow'事件!');
}
函数createPages()
{
$header=“…”;
$content=“…”;
$footer=“…”;

对于($i=1;$i),将jquery核心从

然后

$(selector).live( eventName, function(){} );
签名后可替换为以下内容

$(document).on( eventName, selector, function(){} );

然后,它适用于动态元素。

您可以将代码发布到您试图附加事件和调用的位置吗?javascript代码在哪里?在每个新的动态页面中?还是第一次加载的页面(应该在哪里)?或和示例链接/代码也很好。动态页面有ID#fav1、#fav2、#fav3等。只有一个js文件,所有工作都在其中完成。我试图将事件处理程序简单地附加到每个页面上:$(“#fav1”).live(“tap”,function(event,ui){console.log(“点击处理程序正在工作”)});谢谢Jasper,您关于在动态页面创建时直接绑定处理程序的建议正在发挥作用。关于在动态生成页面上更改某些标记时如何继续的建议?使用我上面提到的ID的标准方法:$(“#fav1 ul”)。追加(
  • 123
  • )在动态生成的页面添加到DOM后,我必须查看呈现的HTML结构。我不能确定如何选择元素,因为我不知道HTML的结构。