javascript - JQM - 如何克服 TypeError : t. 数据(...)未定义

标签 javascript jquery jquery-mobile jquery-mobile-listview

在 JQuery Mobile 中,我创建了一个动态 ListView ,它应该根据单击的项目创建动态页面。我设法启动并运行了 ListView ,但是由于这个错误,动态页面问题让我很头疼:

TypeError: t.data(...) 未定义

...ollapsiblebound",!0).bind("展开折叠",function(t){var n=t.type==="collapse...

每次我尝试导航到动态页面时,我都会在 Firebug 上看到这个。我用来创建 ListView 的代码是这个(它似乎工作正常):

for (i=0; i<contacts_list.length;i++) {
            var patient = contacts_list[i];
            output += "<li id=" + patient.id + "><a href='#update?patient=" + patient.id + "'><h2>" + patient.name + "</h2><a href='#' data-rel='popup' data-position-to='window' data-transition='pop'></a></li>";
        }
    }

$("#patlist").append(output).listview("refresh");

我用来通过单击 ListView 中的项目来创建页面的代码与此页面上的代码非常相似:http://jquerymobile.com/demos/1.1.1/docs/pages/page-dynamic.html

$(document).bind("pagebeforechange", function (e, data) {

 // We only want to handle changePage() calls where the caller is
 // asking us to load a page by URL.
 if (typeof data.toPage === "string") {

     // We are being asked to load a page by URL, but we only
     // want to handle URLs that request the data for a specific
     // category.
     var u = $.mobile.path.parseUrl(data.toPage),
         re = /^#update/;

     if (u.hash.search(re) !== -1) {

         // We're being asked to display the items for a specific category.
         // Call our internal method that builds the content for the category
         // on the fly based on our in-memory category data structure.
         showPatient(u, data.options);

         // Make sure to tell changePage() we've handled this call so it doesn't
         // have to do anything.
         e.preventDefault();
     }
   }
});

function showPatient(urlObj, options) {
 var patientId = urlObj.hash.replace(/.*patient=/, ""),

     // Get the object that represents the category we
     // are interested in. Note, that at this point we could
     // instead fire off an ajax request to fetch the data, but
     // for the purposes of this sample, it's already in memory.
     patient = JSON.parse(storage.getItem("patients:" + patientId)),

     // The pages we use to display our content are already in
     // the DOM. The id of the page we are going to write our
     // content into is specified in the hash before the '?'.
     pageSelector = urlObj.hash.replace(/\?.*$/, "");

 if (patient) {
     // Get the page we are going to dump our content into.
     var $page = $(pageSelector),

         // Get the header for the page.
         $header = $page.children(":jqmData(role=header)"),

         // Get the content area element for the page.
         $content = $page.children(":jqmData(role=content)"),

         // The markup we are going to inject into the content
         // area of the page.
         markup = "<p>" + patient.name + "</p><ul data-role='listview' data-inset='true'>",

         // The array of items for this category.
         cItems = patient.name,

         // The number of items in the category.
         numItems = 1;

     // Generate a list item for each item in the category
     // and add it to our markup.
     for (var i = 0; i < numItems; i++) {
         markup += "<li>" + cItems + "</li>";
     }
     markup += "</ul>";

     // Find the h1 element in our header and inject the name of
     // the category into it.
     $header.find("h1").html(patient.name);

     // Inject the category items markup into the content element.
     $content.html(markup);

     // Pages are lazily enhanced. We call page() on the page
     // element to make sure it is always enhanced before we
     // attempt to enhance the listview markup we just injected.
     // Subsequent calls to page() are ignored since a page/widget
     // can only be enhanced once.
     $page.page();

     // Enhance the listview we just injected.
     $content.find(":jqmData(role=listview)").listview();

     // We don't want the data-url of the page we just modified
     // to be the url that shows up in the browser's location field,
     // so set the dataUrl option to the URL for the category
     // we just loaded.
     options.dataUrl = urlObj.href;

     // Now call changePage() and tell it to switch to
     // the page we just modified.
     $.mobile.changePage($page, options);
  }
}

最佳答案

您忘记创建一个静态页面,您希望在其中动态附加您的项目。如果需要,您可以动态创建一个页面,在导航到它之前,选择一个列表项。

if ($('body').find('[data-role=page]#update').length === 0) {
  $('<div/>', {
    'data-role': 'page',
     id: 'update',
    'data-theme': 'e'
  }).appendTo('body');
}

关于javascript - JQM - 如何克服 TypeError : t. 数据(...)未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18817254/

相关文章:

JavaScript 无法识别 HTML 中的元素

javascript - 使用 JQuery 验证包含内容

javascript - 暂停和恢复ajax重复请求

iphone - 从手机上传到Angular的图像是横向的或上下颠倒的

jquery mobile vmousemove 在需要时平滑

javascript - 桥+命令模式

javascript - 作为汽车配置器控制复杂的 Sprite 图像

javascript - 仅当某些页面在 Angular 2 中设置为默认页面时,应用程序才会中断(仅限 Chrome)

javascript - 链接 ajax jquery 调用的两种方法。正确对象,真爱?

jquery - 过渡 jquery mobile 时导航栏未修复