【问题标题】:Why don't these need to be defined here?为什么这些不需要在这里定义?
【发布时间】:2012-12-29 19:01:11
【问题描述】:

我正在查看 Google 提供的 API,我需要将它翻译成 jQuery,所以我做到了。在 Google 的代码中,Google 定义了创建的元素,但它无需在 jQuery mobile 中定义它们就可以工作。我是编程新手,所以我不确定这是否重要?该代码在控制台日志上没有错误,无需定义。

谷歌:

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

【问题讨论】:

  • 您的 jQuery 无法正常工作,因为您在代码中设置了 ALL a 标签和 ALL li 标签。

标签: javascript jquery variables requirejs-define


【解决方案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

【讨论】:

  • 谢谢!实际上,我实际上在使用 panoramio 时遇到了麻烦。当我从索引导航(我正在使用 jQueryMobile)到地图页面时,我收到一条错误消息:Uncaught TypeError: Cannot read property 'PanoramioLayer' of undefined。它与出现在我发布的原始代码上方的以下代码有关:var panoramioLayer = new google.maps.panoramio.PanoramioLayer();下面是:panoramioLayer.setMap(map);
  • @snowhite 我认为那应该是一个新问题
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2010-12-15
    • 2019-01-16
    • 2021-10-22
    相关资源
    最近更新 更多