2012-12-29 90 views
0

我在看Google提供的API,我需要将它翻译成jQuery,所以我做了。在谷歌的代码中,谷歌定义了创建的元素,但它的工作原理没有在jQuery手机中定义它们。我是编程新手,所以我不确定这是否重要?该代码在控制台日志中无误地运行,无需定义。为什么这些不需要在这里定义?

谷歌:

google.maps.event.addListener(panoramioLayer, 'click', function(photo) { 
    var li = document.createElement('li'); 
    var link = document.createElement('a'); 
    link.innerHTML = photo.featureDetails.title + ': ' + 
     photo.featureDetails.author; 
    link.setAttribute('href', photo.featureDetails.url); 
    li.appendChild(link); 
}); 

的jQuery:

google.maps.event.addListener(panoramioLayer, 'click', function(photo) { 
    $(document.createElement("a")).html("photo.featureDetails.title + ': ' + photo.featureDetails.author"); 
    $("a").attr("href", photo.featureDetails.url); 
    $("li").append("a"); 
}); 
+3

由于您正在设置** ALL ** a标记和** ALL,因此您的jQuery不会工作得太好**代码中的li标签。 –

回答

1

正确的转换应该是这样的: -

google.maps.event.addListener(panoramioLayer, 'click', function(photo) { 
    var anchor=$("<a/>").html(photo.featureDetails.title + ': ' + photo.featureDetails.author).attr("href", photo.featureDetails.url); 
    $("<li/>").append(anchor); 
}); 
1

像这样的东西应该工作:

google.maps.event.addListener(panoramioLayer, 'click', function(photo) 
{ 
    var $link = $(document.createElement("a")).html(photo.featureDetails.title + ': ' + photo.featureDetails.author); 
    $link.attr("href", photo.featureDetails.url); 
    $("<li/>").append($link); 
}); 

您需要存储创建的链接标记,以便您不会更改所有标记的hrefs

+0

谢谢!实际上,我实际上在实际上遇到了一般的panoramio问题。当我从索引导航(我使用jQueryMobile)到地图页面时,出现错误: Uncaught TypeError:无法读取未定义的属性'PanoramioLayer'。 它与下面的代码出现在我发布的原始代码上方: var panoramioLayer = new google.maps.panoramio.PanoramioLayer(); 以下是: panoramioLayer.setMap(map); – snowhite

+0

@snowhite我认为那应该是一个新问题 –