【问题标题】:contents() in <object type="text/html"></object>?<object type="text/html"></object> 中的内容()?
【发布时间】:2009-12-30 16:54:36
【问题描述】:

我可以使用

访问在 &lt;iframe&gt; 中加载的本地内容

$("#frame").contents().find('div').css(...)

当使用 &lt;object type="text/html"&gt; 代替时(同一本地站点),内容功能不起作用。

还有其他方法还是我错过了什么?

下面是测试代码:

HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <link type="text/css" href="css/style.css" rel="stylesheet"/>
    <script type="text/javascript" src="js/jquery-1.3.2-min.js"></script>   
    <script type="text/javascript" src="js/try.js"></script>
</head>
<body>
    <div id="header"></div>
    <div id="here_goes_a_proprietary_side_i_dont_want_to_mess_inside">
        <object id="frame" type="text/html" data="/nastysite/index.php" width="100%" height="100%"></object>
    </div>
    <div id="footer"></div>
</body>

JS:

$(document).ready(function() {

    alert("go ?");  //temporary solution to wait until everything is loaded.
    $("#frame").contents().find('div').css("background-color", "red"); //nothing appens
    console.debug($("#frame").contents().find('div'));  //nothing
});

【问题讨论】:

  • iframe 是否从不同的域加载?我相信 JavaScript 不允许这样做。使用可在本地加载内容的 iframe 尝试您的代码。
  • 是本地内容。
  • 如果你已经在使用 jquery 为什么不直接使用$(targetElement).load('/nastysite/index.php');?它似乎比使用对象方法简单得多。
  • 这是我的第一个想法,但是这个站点包含一个带有很多插件的旧版本的 jquery。当然,我尝试了 noConflict() 和 noConflict(true),但没有取得很大成功。
  • 你有没有想过这个问题?我也有同样的问题。

标签: javascript jquery object-tag


【解决方案1】:

您的对象标签会立即关闭,因此它没有要查找的子 div。

<object id="frame" type="text/html" data="/nastysite/index.php" width="100%" height="100%"></object>

【讨论】:

  • 虽然他需要使用完整的结束标签而不是短语法,但我仍然不确定它是否会暴露他正在“导入”的页面的 DOM。
  • 用short语法试过了,但没有更多效果(网站仍然加载成功)。
猜你喜欢
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 2014-11-08
  • 1970-01-01
  • 2013-01-04
  • 2015-09-21
  • 1970-01-01
  • 2017-08-09
相关资源
最近更新 更多