【问题标题】:javascript variable with a-tag (string build)带有 a-tag 的 javascript 变量(字符串构建)
【发布时间】:2017-05-08 14:23:43
【问题描述】:

我以编程方式插入到编辑器中的这个标记有问题。我不知道如何添加 target="_blank" 属性。即使我尝试了 '" "' 或 "' "' 以及各种组合,我也无法让它工作。我也不知道这个任务我叫什么,所以我不能真正用谷歌搜索它。 任何帮助将不胜感激,以使这项工作

var html = "<a href='" + host + "/draught/connect/Download" + draughtDownloadType + "?PropId=" + selectedPropertyId + autoUpdateQuery + "' target=_blank>" + linkText + "</a>";

【问题讨论】:

    标签: javascript string url concatenation


    【解决方案1】:

    您需要在 " 字符前面放置一个 \ 来转义它。

    target=\"_blank\"
    

    给你:

    var html = "<a href='" + host + "/draught/connect/Download" + draughtDownloadType + "?PropId=" + selectedPropertyId + autoUpdateQuery + "' target=\"_blank\">" + linkText + "</a>";
    

    【讨论】:

    • 谢谢!这非常有效。这正是我想做的。但我不知道它被称为逃生所以我无法谷歌它:)
    • 不用担心@Phil_TheAverage - 搜索语法总是很困难!
    【解决方案2】:

    使用转义

    var html = "<a href='" + host + "/draught/connect/Download" + draughtDownloadType + "?PropId=" + selectedPropertyId + autoUpdateQuery + "' target=\"_blank\">" + linkText + "</a>";
    

    【讨论】:

      【解决方案3】:

      您可以使用 target='_blank'。在 JS 中进行连接时,您可以在 "" 内使用 ' 没有任何问题,反之亦然。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-30
        • 1970-01-01
        • 2019-07-03
        • 1970-01-01
        • 1970-01-01
        • 2011-06-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多