【问题标题】:altering contents inside iframe and remove unwanted elements更改 iframe 内的内容并删除不需要的元素
【发布时间】:2017-03-01 07:54:49
【问题描述】:

我有一个 div

<div id="container">
<iframe id="survey" src="somepage"></iframe>
</div>

现在页面在 iframe 中加载

我想删除 iframe 中除 div 之外的所有其他项目

$("#container").html($("#survey .requiredDiv").html())

但这不起作用

有可能做这样的事情吗?应该怎么做?

【问题讨论】:

标签: javascript jquery


【解决方案1】:

记住:iframesrc 也必须在同一个域中。这是出于安全原因。

但是你可以使用 load/html 这样的方式:

$("#container").html(function(){
    // There could be multiple target elements, i guess you should loop and return the html
    var htm = '';
    $("#survey").contents().find(".requiredDiv").each(function(){
       htm += this;  
    });
    return htm;
});

使用.load()

$("#container").load($("#survey").contents().find(".requiredDiv"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 2016-06-17
    相关资源
    最近更新 更多