gpt4 book ai didi

javascript - 离开页面时内容被删除

转载 作者:行者123 更新时间:2023-11-28 21:15:20 25 4
gpt4 key购买 nike

我目前正在开发一个相当基本的电话间隙应用程序。在一页上,我从 JSON 对象填充 ListView 。它填充得很好,我可以单击列表转到下一页。当我点击android后退按钮(这会让你回到上一页)时,问题就出现了,加载的内容并没有消失。据我所知,一旦加载新页面,DOM 元素就会消失(但是我是这项技术的新手,所以我可能是错的)。

这是 html

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>PhoneGap Demo With JQuery Mobile</title>
<link rel="stylesheet" href="jquery.mobile/jquery.mobile-1.0b2.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="pgandjqm-style-override.css" type="text/css" charset="utf-8" />
<script type="text/javascript" src="jquery.mobile/jquery-1.6.2.min"></script>
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script src="jquery.mobile/jquery.mobile-1.0b2.js"></script>
<script type="text/javascript" charset="utf-8" src="main.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

</head>
<body onload="init();" data-theme="a">
<div data-role="page" data-theme="a">
<div data-role="header">
<img src="images/joycard.png" alt="JoyCard" class="banner">
</div>
<div data-role="navbar">
<ul>
<li><a onclick="categoriesList();" class="ui-btn-active">KATEGORIEN</a></li>
<li><a onclick="mapDetails();">IN DER NÄHE</a></li>
<li><a onclick="allPartners();">ALLE PARTNER</a></li>
</ul>
</div><!-- /navbar -->


<div id="list" data-role="content" data-theme="a">
<ul data-role="listview" data-inset="true" data-theme="a">
</ul>
</div><!-- end jqm content -->

<div id="allPartners" data-role="content">
<ul data-role="listview" data-inset="true" data-theme="a">
</ul>
</div><!--/all partners list -->

<div id="loctext" ></div>

<div id="map_canvas" class="mapView"></div>

<div id="menu" data-role="content" class="menu" onClick="hideMenu();">
<a href="news.html" class="menu-item"><img src="images/news.png" alt="News"></a>
<a href="contact.html" class="menu-item"><img src="images/contact.png" alt="Contact"></a>
<a href="info.html" class="menu-item"><img src="images/info.png" alt="Info"></a>
</div>
</div>
</body>
</html>

和 js/query

var menuUp = false;
var gotLoc = false;
var waitingForLoc = false;
var allWaitingForLoc = false;
var currentId;
var venueId;
var onDetails = false;
var onDetail = false;
var neighborhoods = [];
var markersIds = [];
var mapLoaded = false;

var latlng;
var myLat;
var myLong

document.addEventListener("menubutton", onMenuKeyDown, false);

function init() {



$("#map_canvas").hide();
$("#allPartners").hide();
$("#menu").hide();

var suc = function(position) {
gotLoc = true;
latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
myLat = position.coords.latitude;
myLong = position.coords.longitude;

if (waitingForLoc == true) {
mapDetails();
} else if (allWaitingForLoc == true) {
allPartners();
}

}

var err = function(error) {
alert("Problem with GPS" + error.message);

}

var geoOptions = {
enableHighAccuracy: true,
}


getCategories();

navigator.geolocation.getCurrentPosition(suc, err, geoOptions);

}

function categoriesList() {
$("#allPartners").hide();
$("#map_canvas").hide();
$("#list").show();
}

function allPartners() {

if (gotLoc == true) {

$('#allPartners ul').empty();

$.getJSON("http://www.aURLiamusing.com" +
"&lat=" + myLat + "&lon=" + myLong,
function(data){
$.each(data, function(key, value){
$('<li id="'+ value.id +'" class="list-item-li" onclick="changeToDiscountDetails(this);" data-theme="a">' + value.venue + '</li>').appendTo("#allPartners ul");
});
});


$("#map_canvas").hide();
$("#list").hide();
$("#allPartners").show();

} else if (gotLoc == false) {
$("#list").hide();
jQuery("#loctext").append("Getting geolocation . . .");
waitingForLoc = false;
mapWaitingForLoc = true;
}

}

//change for Iphone
function onMenuKeyDown() {
if (menuUp == true){
$("#menu").hide();
menuUp = false;
} else {
$("#menu").show();
menuUp = true;
}

}

function mapDetails() {


if (mapLoaded == false) {
if (gotLoc == true) {

var mapListURL = "http://www.aURLiamusing.com" +
"&lat=" + myLat + "&lon=" + myLong;
console.log("this is just before the getJson");
$.getJSON(mapListURL,
function(data){
$.each(data, function(key, value){
var pos = new google.maps.LatLng(value.lat, value.lon);
neighborhoods[key] = pos;
markersIds[key] = value.id;
});
setMap();
});

}

if (gotLoc == false) {
$("#list").hide();
jQuery("#loctext").append("Getting geolocation . . .");
allWaitingForLoc = false;
waitingForLoc = true;

}
} else if (mapLoaded == true) {
$("#allPartners").hide();
$("#list").hide();
$("#map_canvas").show();

}


}

function setMap() {

var iterator = 0;
var myOptions = {
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
scaleControl: true,
streetViewControl: false,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
},

zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,

};

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var myMarker = new google.maps.Marker({
position: latlng,
map: map,
title:"Hello World!",
});
myMarker.setMap(map);
for (var i = 0; i < neighborhoods.length; i++) {

var marker = new google.maps.Marker({
position: neighborhoods[i],
map: map,
title:"Hello World!",

});
marker.set("id", markersIds[i]);

marker.setMap(map);
marker.metadata = {type: "point", id: i};

google.maps.event.addListener(marker, 'click', function() {
var val = this.get("id");
venueId = val;
console.log(val);
onDetails = false;
onDetail = true;
$.mobile.changePage("discountDetails.html", { transition: "slide"});

});


}

mapLoaded = true;

$("#allPartners").hide();
$("#list").hide();
$("#map_canvas").show();



}

function hideMenu(){
$("#menu").hide();
}

$('div').live('pageshow',function(event, ui){



if (onDetails == true) {
getVenues();

} else if(onDetail == true) {
populateDetail();

}

});

function populateDetail() {

var url = "http://www.aURLiamusing.com";

$.getJSON(url,
function(data){
$.each(data, function(key, value){
$("#details").append('<h2 data-theme="a">' + value.venue + '</h2>');
var picUrl = value.image;
picUrl.replace(/\/$/, '');
$("#details").append('<img data-theme="a" src="' + picUrl + '"></p>');
$("#details").append('<p data-theme="a">' + value.description + '</p>');
$("#details").append('<p data-theme="a">' + value.offer1 + '</p>');
$("#details").append('<p data-theme="a">' + value.offer2 + '</p>');
});
});


}

function getCategories() {

$('#list ul').empty();

$.getJSON("http://www.aURLiamusing.com",
function(data){
$.each(data, function(key, value){
$('<li id="'+ value.id +'" class="list-item-li" onclick="changeToDetails(this);" data-theme="a">' + value.title + '</li>').appendTo("#list ul");
});
});

}

function getVenues() {


var url = "http://www.aURLiamusing.com;

$.getJSON(url,
function(data){
$.each(data, function(key, value){
$('<li id="'+ value.id +'" class="list-item-li" onclick="changeToDiscountDetails(this);" data-theme="a">' + value.venue + '</li>').appendTo("#venueList ul");
});
});

}


function changeToDetails(object) {
currentId = $(object).attr('id');
onDetails = true;
$.mobile.changePage("details.html", { transition: "slide"});
}

function changeToDiscountDetails(object) {
venueId = $(object).attr('id');
onDetails = false;
onDetail = true;
$.mobile.changePage("discountDetails.html", { transition: "slide"});
}

感谢您的帮助

最佳答案

尝试使用

data-dom-cache="true" in the < div data-role="page" > tag

关于javascript - 离开页面时内容被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7752539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com