【问题标题】:Google Maps infowindow.setContent conditionals谷歌地图 infowindow.setContent 条件
【发布时间】:2017-06-22 02:07:26
【问题描述】:

我正在从 JSON 创建一些 Google 地图标记,并且我想使用简写 if 语句来格式化内容。这是我现在拥有的:

infowindow.setContent('<strong>' + this.title + '</strong>' + this.description + '<a href="' + this.link + '" target="_blank">Website</a>');

所有标记都有标题,但有些可能有描述但没有链接或链接但没有描述。我怎样才能做到这一点,如果标记有链接和描述,它看起来像上面,如果它有一个链接但没有描述,它看起来像这样:

infowindow.setContent('<strong>' + this.title + '</strong>' + '<br/>' + '<a href="' + this.link + '" target="_blank">Website</a>');

如果它有描述但没有链接,则像这样:

infowindow.setContent('<strong>' + this.title + '</strong>' + this.description );

我知道我可以设置 3 个不同的 infowindow.setContent 函数,但我想知道是否有一种方法可以使用内联条件语句对一个函数执行此操作。

【问题讨论】:

    标签: jquery google-maps conditional


    【解决方案1】:

    你可以使用三元运算符

    infowindow.setContent('<strong>' + this.title + '</strong>' + 
        ( this.description =='' ?  ('<br/><a href="' + this.link +
            '" target="_blank">Website</a>') : this.description))  ;
    

    【讨论】:

    • 正是我想要的,谢谢!您在分号前缺少),但添加后它可以正常工作。
    • @user13286 ... 非常感谢 .. 答案在分号前用 ) 更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    相关资源
    最近更新 更多