Как я обращаюсь к объекту документа <iframe> от родительской страницы, с помощью jQuery?

Некоторые расширения ActionLink: http://www.squaredroot.com/post/2008/06/11/MVC-and-SSL.aspx Или атрибут действия контроллера, который перенаправляет к https:// http://forums.asp.net/p/1260198/2358380.aspx#2358380

9
задан Bungle 16 October 2009 в 16:48
поделиться

4 ответа

Вы дождались загрузки iframe?

В любом случае, следующий код работает в FF3.5, Chrome 3, IE6, IE7, IE8.
Размещенная демонстрация: http://jsbin.com/amequ (редактируется через http://jsbin.com/amequ/edit )

<iframe src="blank.htm" ></iframe> 

<script> 
  var $iframe = $('iframe'); 

  /*** 
  In addition to waiting for the parent document to load 
  we must wait for the document inside the iframe to load as well. 
  ***/ 
  $iframe.load(function(){       
    var $iframeBody = $iframe.contents().find('body'); 

    alert($iframeBody.height()); 

    /*** 
    IE6 does not like it when you try an insert an element 
    that is not made in the target context. 
    Make sure we create the element with the context of 
    the iframe's document. 
    ***/ 
    var $newDiv = $('<div/>', $iframeBody.get(0).ownerDocument); 
    $newDiv.css('height', 2000); 

    $iframeBody.empty().append($newDiv); 

    alert($iframeBody.height()); 
  }); 
</script>
4
ответ дан 4 December 2019 в 22:28
поделиться

Мне пришлось сделать это в недавнем проекте, и я использовал это:

<iframe src="http://domain.com" width="100%" 
border="0" frameborder="0" id="newIframe"
 onload="$(this).css({height:this.contentWindow ? this.contentWindow.document.body.scrollHeight + (20) : this.contentDocument.body.scrollHeight + (20)});" 
style="border:none;overflow:hidden;">

</iframe>

Хотя у меня есть встроенная функция (имеет смысл в моем проекте, поскольку iframe создается с помощью PHP). Если вы добавляете iframe через javascript, это будет что-то вроде

$("#container").append('<iframe src="http://domain.com" width="100%" border="0" frameborder="0" id="newIframe" style="border:none;overflow:hidden;"></iframe>');

$("#newIframe").load(function(){
  var _this=$("this")[0];
  $(this).css({
       height:
        _this.contentWindow ? _this.contentWindow.document.body.scrollHeight + (20) : _this.contentDocument.body.scrollHeight + (20)});
});

//contentWindow.document.body is for IE
//contentDocument.body is for everything else
0
ответ дан 4 December 2019 в 22:28
поделиться

Есть много способов получить доступ к iframe:

Например, у нас есть эта HTML-страница:

<iframe name="my_frame_name" id="my_frame_id">

И JS:

frames[0] == frames['my_frame_name'] == document.getElementById('my_frame_id').contentWindow  // true
0
ответ дан 4 December 2019 в 22:28
поделиться
$('iframe').contents().get(0)

or

$('iframe').contents()[0]

will return the document of the first iframe on the page. Remember jQuery objects are arrays of the matching DOM elements

1
ответ дан 4 December 2019 в 22:28
поделиться
Другие вопросы по тегам:

Похожие вопросы: