如何使用 javascript 或 jQuery 获取 iframe 之外的元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23913951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:47:56  来源:igfitidea点击:

How to get an element outside of iframe with javascript or jQuery?

javascriptjqueryhtmliframe

提问by Adrian Axinte

I'm trying to get a element from outside of iframe with js or jQuery. I found something but it's not cross browser.

我正在尝试使用 js 或 jQuery 从 iframe 外部获取元素。我找到了一些东西,但它不是跨浏览器。

<div id=vi-desc-maincntr"">
    <div class="u-flL iti-act-num">123456789</div>
    <div id="desc_div">
        <iframe src="LOCATION">
    </div>
</div>

I need that number into a variable.

我需要将该数字转换为变量。

回答by naota

Same-origin policy:
You might want to check this. Two pages need to have the same origin.
https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

同源策略
您可能需要检查一下。两个页面需要具有相同的来源。
https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy



如果您的子页面与父页面位于同一域,您可以在子窗口中编写如下代码:

 $('#test1', parent.document).html('<h1>clicked</h1>');

The second parameter provides the contextin which to search the element matched by the first parameter. The Document is here:http://api.jquery.com/jQuery/#jQuery-selector-context

第二个参数提供了context搜索与第一个参数匹配的元素的位置。文档在这里:http: //api.jquery.com/jQuery/#jQuery-selector-context

 jQuery( selector [, context ] )

Hope this helps.

希望这可以帮助。