从iframe内容中获取父元素的属性值 [英] get attribute value of parent element from iframe content
问题描述
如果我在 iframe $ c $中运行脚本,是否有办法获取属性
data-media
的值c>内容?
Is there a way to get the value of attribute data-media
if I'm running the script inside the iframe
content?
代码如下所示:
<div id="myDiv">
<iframe data-create-resource-url="http://my.domain.url"
data-media="Song"
frameborder="0"
height="41"
src="https://different.domain.url" width="366">
</iframe>
</div>
我已经尝试了很多方法,例如 window.parent.document
或 top.document
或 window.parent
和其他可用的解决方案,但似乎没有上班。
I've tried a lot of ways already like window.parent.document
or top.document
or window.parent
and other available solutions but doesn't seem to work.
推荐答案
嗯,你真的不需要访问父母。在iframe中运行脚本时。 iframe当前是iframe中脚本的窗口。所以访问window元素应该给你说 attr
。试试这个(未测试):
Well you dont really need to access the parent. As you are running the script inside iframe. iframe is currently the window for your script inside iframe. So accessing window element should give you the said attr
. Try this (not tested):
alert($(window).attr('data-media'));
这篇关于从iframe内容中获取父元素的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!