从iframe内容中获取父元素的属性值 [英] get attribute value of parent element from iframe content

查看:89
本文介绍了从iframe内容中获取父元素的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 iframe data-media 的值c>内容?

Is there a way to get the value of attribute data-media if I'm running the script inside the iframecontent?

代码如下所示:

<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆