簡體   English   中英

jQuery-在iframe內的按鈕的onclick事件中更改iframe高度

[英]jQuery - Change Iframe height in the onclick event of a button inside the iframe

現在,我正在開發一個簡單的HTML / jQuery腳本,當我單擊由iframe自身加載的按鈕時,我想更改iframe的高度。

看一下我的代碼:

index.php文件:

<div id="outerdiv">
    <iframe src="http://beta.sportsdirect.bg/test/iframe.php" id="inneriframe" scrolling="no"    target="_parent"></iframe>
</div>

iframe.php:

 <HTML>
 <head>
 <script src="//code.jquery.com/jquery-1.10.2.js"></script>
 </head>
    <script type="text/javascript">
    $("#SuperWebF1").click(function(){

        $('#inneriframe, #outerdiv', window.parent.document).css({"height":"450px"});

    })
    </script>
 <div style="display:block;height:300px;">
    <h3>The iframe content</h3>  
        <button type="button" id="SuperWebF1">Click me to resize the holding Iframe!</button>
  </div>        

這是index.php的演示: http : //beta.sportsdirect.bg/test/

當您打開它時,您會看到iframe加載的按鈕。 當您點擊按鈕時,iframe不會改變高度!

為什么會出錯?

你能幫我使這件事起作用嗎?

提前致謝!

這是我對您的代碼所做的一些更改。

//function call needed to make sure page is loaded before javascript runs
$(document).ready(function(){//begin document ready function

//on click event
$(document).on("click","#SuperWebF1",function(){//begin on click event

    //change the height of selected elements
    $('#inneriframe, #outerdiv', window.parent.document).css({"height":"450px"});

    });//end on click event


  });//end document ready function

您需要先獲取iframe的內容,然后才能設置點擊事件。 嘗試這個 -

var iframeDoc = $('#inneriframe').contents().get(0);
$(iframeDoc).on('click', 'SuperWebF1', function() {
       ........... your code here  .............
});

快樂編碼!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM