【问题标题】:find <Object> src or <embed> src inside <iframe> using javascript使用 javascript 在 <iframe> 中查找 <Object> src 或 <embed> src
【发布时间】:2023-04-08 05:02:01
【问题描述】:

我想访问和数据值并将 src vlaue 嵌入到 iframe 内的 html 页面内的 object 标记内。如下图

<body>
 
<iframe id="if20" width="300" height="250">
<!DOCTYPE>
	<html>
		<head>
			<title>Test</title>
        </head>
<body>
<object type="" data="" width="300" height="250" id="fw1"><param name="" value="opaque">
<param name="" value="always">
<param name="quality" value="high">
<param name="flash vars" value=">
</object>	
<object width="670" height="369">

<param name="ABC" value="always">
<param name="movie" value="">
<embed src="abc.swf" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="670" height="369">
</object>
</body>
</html>
</iframe>
</body>

那么我可以访问对象标签的值吗??

【问题讨论】:

  • 尝试了什么?
  • 你不想使用jQuery吗?认真的吗?
  • iframe 是否在同一个域中?
  • 我想使用 JavaScript。并且 iframe 内容可能属于其他域

标签: javascript html object


【解决方案1】:

如果您不违反CORS 规则,您只能访问 iframe 内部的值。这意味着,iframe 必须在同一个域中,否则这将不起作用。

如果 iframe 在同一个域中,则可以在 DOM 准备好后通过以下行访问它。

var iframe = document.getElementById("if20");

此时 iframe 还没有准备好。它必须完成加载:

iframe.onload = function(){ /* further code goes here */ };

在回调中,您可以访问 iframe 中的元素,例如:

var objectEl = iframe.contentDocument.getElementById("fw1");

如果您需要 iframe 的窗口对象,请使用 iframe.contentWindow

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 2015-11-22
    • 2011-10-02
    • 2015-05-29
    • 1970-01-01
    • 2012-01-28
    • 2014-10-27
    相关资源
    最近更新 更多