簡體   English   中英

Cordova / Phonegap — jQuery不執行Javascript <div> 加載

[英]Cordova / Phonegap — jquery not executing Javascript on <div> load

我有一個用於phonegap的頁面(index.html)文檔,當每個新的“ div”被引用時,該頁面顯示不同的“頁面”。 例如:

<div class="upage ui-page-theme-a" id="view_history" data-role="page" >

我想讓顯示“ div”時通過Jquery自動執行的javascript函數。 我的代碼如下。

但是我什么也無法使它在div加載時執行。 我可以在HTML中放置一個“ onclick”調用,即:

<a onclick="listMeasurements()" class="button" style="" data-role="button">List 
Current Measurements</a>

效果很好,但是由於某些原因,我無法讓它僅通過加載來執行。 任何見解將不勝感激。

如果我忽略了一些明顯的事,請提前抱歉。

$('#view_history').on('pagecontainershow', listMeasurements()) ;

// I've also tried  bind() etc.



function listMeasurements() {
"use strict" ;
var fName = "listMeasurements():" ;

var measurements_recorded = window.localStorage.length;


$("#Measurements").html("<strong>" + measurements_recorded + "</strong> 
measurement(s) recorded");

// Empty the list of recorded tracks
$("#measurement_list").empty();

// Iterate over all of the recorded tracks, populating the list
       for(var i=0; i<measurements_recorded; i++){

 $("#measurement_list").append("<li data-uib=\"jquery_mobile/listitem\" data-ver=\"0\"><span><a href=\"#results-graphs\" data-ajax=\"false\">" + window.localStorage.key(i) + "</a></span></li>");

                  }

        $('#measurement_list').listview().listview('refresh'); 
   }

從jQuery Mobile 1.4.2開始,事件已更改,並且無法像您那樣綁定到特定頁面,例如,

$('#view_history').on('pagecontainershow', listMeasurements()) ;

將不再工作。

您需要在文檔上放置類似這樣的綁定-

function showSomeLove() {
  alert("What is love, baby don't hurt me, no more!");
}

$(document).on( "pagecontainershow", function( event, ui ) {
   var currentPageId = $( ":mobile-pagecontainer" ).pagecontainer( "getActivePage" ).attr('id');
   if(currentPageId == 'page-a') {  
     /*Execute or Call your function here*/
      showSomeLove();
   } else if(currentPageId == 'page-b') {
      /*do something else*/
   }
});

這是一個工作示例-http : //codepen.io/nitishdhar/pen/gnqFH

欲了解更多詳情閱讀pagecontainer部件- http://api.jquerymobile.com/pagecontainer/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM