jquery load()

编程入门 行业动态 更新时间:2024-10-18 08:26:22
jquery load() - 无法加载谷歌地图(jquery load() - unable to load google maps)

我无法在使用JQuery中的load()新页面的内容时加载谷歌地图,而无需重新加载窗口。

Jquery ajax调用是:

$("form").submit(function(){ $(".cs-container").load("set-location.php .cs-container"); return false; });

我试图从当前div.cs-container类元素中的另一个set-location.php页面加载.cs-container的内容。 它已成功加载但谷歌地图无法加载 。 g-maps容器放在set-location.php页面的.cs-container中,它的脚本文件包含在该页面的头部。

google maps工作得很好,但不加载在ajax调用。

当我尝试这样的事情时:

$("form").submit(function(){ $(".cs-container").load("set-location.php"); return false; });

它工作得很好,但它在.cs-container加载了整个页面。 我不想要的。

谷歌地图初始化脚本为(在文件cs-loc.js )

/*check if geolocaiton supported*/ if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success); } else { error('Geo Location is not supported'); } function success(position) { var lat = position.coords.latitude; /*getting cordinates*/ var long = position.coords.longitude; var input = document.getElementById("latLong"); input.value = lat + ", " + long; var poos = new google.maps.LatLng(lat,long); /*set lat long for maps*/ var mapProp = { center: poos, zoom:16, mapTypeId:google.maps.MapTypeId.ROADMAP }; var map=new google.maps.Map(document.getElementById("googleMap"),mapProp); /*init maps*/ /*create marker*/ var marker=new google.maps.Marker({ position:poos, title: 'Your Store Location', draggable: true }); marker.setMap(map); /*set marker to map*/ google.maps.event.addListener(marker, 'dragend', function(a) { console.log(a); input.value = a.latLng.lat().toFixed(4) + ', ' + a.latLng.lng().toFixed(4); }); } success();

怎么解决?

I have trouble to load google maps while loading the contents of new page using load() in JQuery without reloading window.

Jquery ajax call is:

$("form").submit(function(){ $(".cs-container").load("set-location.php .cs-container"); return false; });

I am trying to load the contents of .cs-container from another set-location.php page in the current div.cs-container class element. it's loaded successfully but the google maps does't load. The g-maps container is placed in .cs-container in the set-location.php page and it's script files are contained in the head of that page.

google maps works very well but does't load in the ajax call.

when i try something like this:

$("form").submit(function(){ $(".cs-container").load("set-location.php"); return false; });

it works very well but it load the complete page in the .cs-container. that I don't want.

the google maps initializing script is as (in the file cs-loc.js):

/*check if geolocaiton supported*/ if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success); } else { error('Geo Location is not supported'); } function success(position) { var lat = position.coords.latitude; /*getting cordinates*/ var long = position.coords.longitude; var input = document.getElementById("latLong"); input.value = lat + ", " + long; var poos = new google.maps.LatLng(lat,long); /*set lat long for maps*/ var mapProp = { center: poos, zoom:16, mapTypeId:google.maps.MapTypeId.ROADMAP }; var map=new google.maps.Map(document.getElementById("googleMap"),mapProp); /*init maps*/ /*create marker*/ var marker=new google.maps.Marker({ position:poos, title: 'Your Store Location', draggable: true }); marker.setMap(map); /*set marker to map*/ google.maps.event.addListener(marker, 'dragend', function(a) { console.log(a); input.value = a.latLng.lat().toFixed(4) + ', ' + a.latLng.lng().toFixed(4); }); } success();

How it can be solve?

最满意答案

您可以在load()回调中使用$.getScript()

$(".cs-container").load("set-location.php .cs-container", function(){ $.getScript('google.maps....').done(function(){ $.getScript('cs-loc.js'); }); });

You could use $.getScript() in the load() callback

$(".cs-container").load("set-location.php .cs-container", function(){ $.getScript('google.maps....').done(function(){ $.getScript('cs-loc.js'); }); });

更多推荐

本文发布于:2023-08-06 16:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1452804.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:jquery   load

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!