ReferenceError:找不到变量:$,jquerymobile + phonegap

我是新手。 我想保存用户在首选项管理器中输入的值,并让它自动填充一个字段(如果已由用户填写)。 我的Android logcat中出现了“ReferenceError:找不到变量:$ at file:///android_asset/www/index.html:53”。 我的应用程序也崩溃了。 我究竟做错了什么?

     <!---->    Hello World  document.addEventListener("deviceready", onDeviceReady, false); //once the device ready event fires up, you can safely do your thing function onDeviceReady(){ //document.getElementById("welcomeMsg").innerHTML += "Phonegap is ready! version="; } $(function(){ $('#savebutton').click(function(){ //this is where the user manually keys in values, do error checking here window.localStorage.setItem("user_lat", $('#user_lat').val()); }); //called when the page is being shown $('#newpage').live('pageshow', function(){ var userLat = window.localStorage.getItem("user_lat"); if (userLat.length > 0){ $('#user_lat').val(userLat); } }); });    

Home Page

Hola Phonegap & Jquery mobile! Go to new page

Footer

Manually specify your GPS coordinates

Save

Footer

如果未定义’$’,我认为jQuery没有正确加载..

尝试:

 if ( $.mobile ) { // loaded } else { // not loaded } 

检查是否加载了jQuery mobile,或者尝试使用普通jQuery:

 if (!jQuery) { alert("jQuery is not loaded"); } 

你需要在你的页面中包含jQuery才能使用你没有的$(jQuery)。 您可以添加脚本标记并在src属性中提供jquery的url。 在head标记中添加此标记,以便在需要之前添加它。