Javascript 为什么不';这些不需要在这里定义吗?

Javascript 为什么不';这些不需要在这里定义吗?,javascript,jquery,variables,requirejs-define,Javascript,Jquery,Variables,Requirejs Define,我在看Google提供的API,我需要把它翻译成jQuery,所以我做了。在谷歌的代码中,谷歌定义了所创建的元素,但在jquerymobile中没有定义它们。我是编程新手,所以我不确定这是否重要?代码在控制台日志上运行时没有错误,没有定义错误 谷歌: google.maps.event.addListener(panoramioLayer, 'click', function(photo) { var li = document.createElement('li'); var

我在看Google提供的API,我需要把它翻译成jQuery,所以我做了。在谷歌的代码中,谷歌定义了所创建的元素,但在jquerymobile中没有定义它们。我是编程新手,所以我不确定这是否重要?代码在控制台日志上运行时没有错误,没有定义错误

谷歌:

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");
});

正确的转换应如下所示:-

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);
});
google.maps.event.addListener(全景图层,'click',函数(照片){
var anchor=$(“”).html(photo.featureDetails.title+':'+photo.featureDetails.author).attr(“href”,photo.featureDetails.url);
$(“
  • ”)追加(锚定); });
  • 类似的方法应该可以:

    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);
    });
    
    google.maps.event.addListener(全景图层,'click',函数(照片)
    {
    var$link=$(document.createElement(“a”)).html(photo.featureDetails.title+':'+photo.featureDetails.author);
    $link.attr(“href”,photo.featureDetails.url);
    $(“
  • ”)追加($link); });

  • 您需要存储创建的链接标记,这样就不会更改标记的所有HREF

    因为您在代码中设置了所有a标记和所有li标记,所以jQuery将无法正常工作。谢谢!事实上,我在使用panoramio时遇到了麻烦。当我从索引导航到地图页面时(我使用的是jQueryMobile),我会得到一个错误:uncaughttypeerror:cannotreadproperty'PanoramioLayer'of undefined。它与我发布的原始代码上方出现的以下代码有关:var panoramioLayer=new google.maps.panoramio.panoramioLayer();下面是:panoramioLayer.setMap(map)@斯诺海特我想这应该是一个新问题