【问题标题】:Adding existing variable to iFrame URL将现有变量添加到 iFrame URL
【发布时间】:2021-07-12 17:35:49
【问题描述】:

这可能是非常基本的,但我们将不胜感激。

我有以下脚本,可以添加到自定义构建的 Web 应用程序中,并在其中嵌入 iframe 中的另一个页面。

$(function() {
    $('#customframe').after('<iframe src="https://www.myurl.com" style="width:100%;height:200px"></iframe>');
});

我需要在 iframe 中包含一个查询参数,如下所示

$(function() {
    $('#customframe').after('<iframe src="https://www.myurl.com/page.html?id=ThisRecordId" style="width:100%;height:200px"></iframe>');
});

ThisRecordId 变量已在 DOM 中可用。 如果我转到浏览器控制台并输入ThisRecordId,它会显示我需要的 iFrame 值。

我只是在努力让它发挥作用。

提前致谢。

【问题讨论】:

    标签: javascript jquery iframe


    【解决方案1】:

    对字符串文字使用字符串插值, 在${ThisRecordId} 模板中使用反引号而不是引号并将变量括起来 更多关于模板文字here

    示例:

    let ThisRecordId = 'myidnumber'
    
    $(function() {
        $('#customframe').after(`<iframe src="https://www.myurl.com/page.html?id=${ThisRecordId}" style="width:100%;height:200px"></iframe>`);
    });
    

    【讨论】:

    • 非常感谢约翰的帮助
    猜你喜欢
    • 1970-01-01
    • 2018-04-14
    • 2020-05-11
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    相关资源
    最近更新 更多